LeetCode | Remove Duplicates from Sorted Array
2014-04-03 15:16
435 查看
题目
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 =
Your function should return length =
分析
和Remove Element神似
代码
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].
分析
和Remove Element神似
代码
public class RemoveDuplicatesFromSortedArray { public int removeDuplicates(int[] A) { if (A == null || A.length == 0) { return 0; } int count = 1; for (int i = 1; i < A.length; ++i) { if (A[i] != A[count - 1]) { A[count++] = A[i]; } } return count; } }
相关文章推荐
- Leetcode--Remove Duplicates from Sorted Array
- [Leetcode]_26 Remove Duplicates from Sorted Array
- LeetCode 80:Remove Duplicates from Sorted Array II
- 【LeetCode】Remove Duplicates from Sorted Array 解题报告
- [LeetCode-26] Remove Duplicates from Sorted Array(移除数组重复元素)
- Leetcode 之 Remove Duplicates from Sorted Array II
- leetcode26~Remove Duplicates from Sorted Array
- LeetCode 26:Remove Duplicates from Sorted Array
- LeetCode 26 - Remove Duplicates from Sorted Array
- LeetCode解题报告--Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- Leetcode - Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array
- [LeetCode]26. Remove Duplicates from Sorted Array
- LeetCode:Remove Duplicates from Sorted Array I&&II
- leetcode-26 Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array
- leetcode-80-Remove Duplicates from Sorted Array II 顺序表
- 【Leetcode】Remove Duplicates from Sorted Array II
- [leetcode]【数组】26. Remove Duplicates from Sorted Array