Remove Duplicates from Sorted Array
2014-11-11 11:11
183 查看
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A =
[1,1,2],
Your function should return length =
2, and A is now
[1,2].
public class Solution { public int removeDuplicates(int[] A) { if(A.length == 0) return A.length; int B[] = new int[A.length]; int k = 1; B[0] = A[0]; for(int i = 1; i< A.length; i++){ if(A[i] != B[k - 1]){ B[k++] = A[i]; } } System.arraycopy(B, 0, A, 0, k); return k; } }
相关文章推荐
- 26 Remove Duplicates from Sorted Array
- leetcode26 Remove Duplicates from Sorted Array 去重已排序列表
- leetcode-26 Remove Duplicates from Sorted Array
- LeetCode :: Remove Duplicates from Sorted Array II 详细分析
- Remove duplicates from sorted array I
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- LeetCode解题思路之Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- LeetCode::Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- 【Remove Duplicates from Sorted Array II】cpp
- 26. Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II C#
- Remove Duplicates from Sorted Array II
- LeetCode Remove Duplicates from Sorted Array
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (java)
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
- [leetcode]Remove Duplicates from Sorted Array