LeetCode - Remove Duplicates from Sorted Array
2014-01-14 04:03
197 查看
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].
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
Solution:
Since the array is sorted, we can scan from the head and check if A[i] == A[i+1]. And use a new index to store the position. And the final result of index is also the length of the new array. Be careful about the value of the new index and array overflow.
https://github.com/starcroce/leetcode/blob/master/remove_duplicates_from_sorted_array.cpp
// 72 ms for 160 test cases
// Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= 1) {
return n;
}
int index = 1, current = 1;
while(current < n) {
if(A[current] == A[current-1]) {
current++;
continue;
}
A[index] = A[current];
index++;
current++;
}
return index;
}
};
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].
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
Solution:
Since the array is sorted, we can scan from the head and check if A[i] == A[i+1]. And use a new index to store the position. And the final result of index is also the length of the new array. Be careful about the value of the new index and array overflow.
https://github.com/starcroce/leetcode/blob/master/remove_duplicates_from_sorted_array.cpp
// 72 ms for 160 test cases
// Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n <= 1) {
return n;
}
int index = 1, current = 1;
while(current < n) {
if(A[current] == A[current-1]) {
current++;
continue;
}
A[index] = A[current];
index++;
current++;
}
return index;
}
};
相关文章推荐
- leetcode笔记:Remove Duplicates from Sorted Array
- [LeetCode] 26. 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】 080. Remove Duplicates from Sorted Array II
- 【LeetCode】Remove Duplicates from Sorted Array
- Leetcode 26 Remove Duplicates from Sorted Array STL
- 【leetcode c++】26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array (C,C++,Java,Python)
- <LeetCode OJ> 26. Remove Duplicates from Sorted Array
- [LeetCode][JavaScript]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 (移除有序数组中重复数字)
- 【LeetCode】之Remove Duplicates from Sorted Array
- 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