LeetCode – Remove Duplicates from Sorted Array II (Java)
来源:互联网 发布:打印机usb端口设置 编辑:程序博客网 时间:2024/06/10 22:16
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
Naive Approach
Given the method signature "public int removeDuplicates(int[] A)", it seems that we should write a method that returns a integer and that's it. After typing the following solution:
public class Solution { public int removeDuplicates(int[] A) { if(A == null || A.length == 0) return 0; int pre = A[0]; boolean flag = false; int count = 0; for(int i=1; i<A.length; i++){ int curr = A[i]; if(curr == pre){ if(!flag){ flag = true; continue; }else{ count++; } }else{ pre = curr; flag = false; } } return A.length - count; }}
Online Judge returns:
Submission Result: Wrong AnswerInput:[1,1,1,2]Output:[1,1,1]Expected:[1,1,2]
So this problem also requires in-place array manipulation.
Correct Solution
We can not change the given array's size, so we only change the first k elements of the array which has duplicates removed.
public class Solution {public int removeDuplicates(int[] A) {if (A == null || A.length == 0)return 0; int pre = A[0];boolean flag = false;int count = 0; // index for updatingint o = 1; for (int i = 1; i < A.length; i++) {int curr = A[i]; if (curr == pre) {if (!flag) {flag = true;A[o++] = curr; continue;} else {count++;}} else {pre = curr;A[o++] = curr;flag = false;}} return A.length - count;}}
Better Solution
public class Solution {public int removeDuplicates(int[] A) {if (A.length <= 2)return A.length; int prev = 1; // point to previousint curr = 2; // point to current while (curr < A.length) {if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {curr++;} else {prev++;A[prev] = A[curr];curr++;}} return prev + 1;}}
0 0
- LeetCode – Remove Duplicates from Sorted Array II (Java)
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- zoj 2853 Evolution(矩阵快速幂)
- 一辈子不长,活的就是一种心情
- 析构函数
- Keen Team: 这群执著的人和那些纯粹的事
- phpStorm打开提示 failed to create JVM 的解决办法
- LeetCode – Remove Duplicates from Sorted Array II (Java)
- 如何配置一个好用的Android模拟器
- 字典树入门及实现(JAVA)
- Mysql 默认参数的修改
- HTML4和HTML5之间10个关键的不同之处
- 提高图形高效绘图机制的方法--旧事重拾
- java 数组
- android 系统导入和导出文件的命令
- 分析MapReduce执行过程+统计单词数例子