LeetCode-Remove Duplicates from Sorted Array
2014-08-17 19:09
351 查看
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 =
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0 || n == 1) return n;
int begin = 0, end = 1, length = 1;
while (end < n) {
if (A[begin] != A[end]) {
++length;
++begin;
A[begin] = A[end];
}
++end;
}
return length;
}
};</span>
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].
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0 || n == 1) return n;
int begin = 0, end = 1, length = 1;
while (end < n) {
if (A[begin] != A[end]) {
++length;
++begin;
A[begin] = A[end];
}
++end;
}
return length;
}
};</span>
相关文章推荐
- leetcode26~Remove Duplicates from Sorted Array
- LeetCode Remove Duplicates from Sorted Array II移掉重复数值
- [Leetcode]#26 Remove Duplicates from Sorted Array
- leetcode-026 Remove Duplicates from Sorted Array
- leetcode之 Remove Duplicate from Sorted Array
- 4---LeetCode【tag: Array】【Remove Duplicates from Sorted Array】|C语言|总结
- LeetCode Remove Duplicates from Sorted Array
- LeetCode 080 Remove Duplicates from Sorted Array II
- [LeetCode By Python]26. Remove Duplicates from Sorted Array
- [LeetCode]26 Remove Duplicates from Sorted Array
- [LeetCode] 26. Remove Duplicates from Sorted Array
- 【LeetCode-26】Remove Duplicates from Sorted Array
- Java for LeetCode 080 Remove Duplicates from Sorted Array II
- leetcode_26_ Remove Duplicates from Sorted Array (easy)
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode - Remove Duplicates from Sorted Array II
- leetcode Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array
- Leetcode_Remove Duplicates from Sorted Array II
- [Leetcode]Remove Duplicates from Sorted Array II