[LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
2018-03-14 05:42
337 查看
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,1,2,2,3], Your function should return length = [code]5, with the first five elements of
numsbeing
1, 1, 2, 2and 3 respectively. It doesn't matter what you leave beyond the returned length.[/code]
Example 2:
Given nums = [0,0,1,1,1,1,2,3,3], Your function should return length = [code]7, with the first seven elements of
numsbeing modified to
0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length. [/code]
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
26. Remove Duplicates from Sorted Array 的拓展,如果允许最多两次重复,返回去除之后的长度。
解法:同样使用双指针,增加一个变量记录出现的字符数量。
Java:
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 updating int 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; } }
Java:
public class Solution { public int removeDuplicates(int[] A) { if (A.length <= 2) return A.length; int prev = 1; // point to previous int 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; } }
Python:
class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): if not A: return 0 last, i, same = 0, 1, False while i < len(A): if A[last] != A[i] or not same: same = A[last] == A[i] last += 1 A[last] = A[i] i += 1 return last + 1
C++:
class Solution { public: int removeDuplicates(int A[], int n) { if (n <= 2) return n; int pre = 0, cur = 1, count = 1; while (cur < n) { if (A[pre] == A[cur] && count == 0) ++cur; else { if (A[pre] == A[cur]) --count; else count = 1; A[++pre] = A[cur++]; } } return pre + 1; } };
类似题目:
[LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
All LeetCode Questions List 题目汇总
相关文章推荐
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
- 27.[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
- [LeetCode]154 有序数组旋转后的最小值 II
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
- LeetCode - 167. 两数之和 II - 输入有序数组
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
- 【leetcode】Two Sum II - Input array is sorted(输入有序的数组,求两个数的和)
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
- leetcode 两数之和 II - 输入有序数组 python
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
- leetcode 7. 在有序可重复数组旋转后搜索 Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
- [Leetcode] search in rotated sorted array ii 搜索旋转有序数组
- [LeetCode] Remove Duplicates from Sorted Array 从有序数组中删除重复项