[LeetCode] Remove Duplicates from Sorted Array 解题报告
来源:互联网 发布:大汉主义知乎 编辑:程序博客网 时间:2024/05/16 08:16
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 =
Given input array A =
[1,1,2]
,Your function should return length =
2
, and A is now [1,2]
.» Solve this problem
[解题思路]
二指针问题。一前一后扫描。
[Code]
1: int removeDuplicates(int A[], int n) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: int pre, cur;
5: pre = 1; cur = 1;
6: if(n <=1) return n;
7: while(cur<n)
8: {
9: if(A[cur] == A[cur-1])
10: {
11: cur++;
12: continue;
13: }
14: A[pre] = A[cur];
15: pre++;
16: cur++;
17: }
18: return pre;
19: }
Updated. 3/9/2013
1: int removeDuplicates(int A[], int n) {
2: if(n ==0) return 0;
3: int index = 0;
4: for(int i =0;i<n; i++)
5: {
6: if(A[index] == A[i])
7: {
8: continue;
9: }
10: index++;
11: A[index] = A[i];
12: }
13: return index+1;
14: }
0 0
- 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] Remove Duplicates from Sorted Array II 解题报告
- LeetCode 80. Remove Duplicates from Sorted Array II 解题报告
- 【LeetCode】Remove Duplicates from Sorted Array 解题报告
- [Leetcode] 26. Remove Duplicates from Sorted Array 解题报告
- [Leetcode] 80. Remove Duplicates from Sorted Array II 解题报告
- [leetCode 解题报告]026. Remove Duplicates from Sorted Array
- Leetcode Remove Duplicates from Sorted Array 解题报告
- LeetCode---Remove Duplicates from Sorted Array、Remove Duplicates from Sorted Array II解题分析
- Remove Duplicates from Sorted Array ——解题报告
- Remove Duplicates from Sorted Array I、II解题报告
- Leetcode #26. Remove Duplicates from Sorted Array 移除重复数字 解题报告
- Leetcode 80. Remove Duplicates from Sorted Array II 重复移除 解题报告
- 【LeetCode】remove-duplicates-from-sorted-list and remove-duplicates-from-sorted-list ii 解题报告
- leetcode解题集锦之Remove Duplicates from Sorted Array
- [LeetCode] Plus One 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node II 解题报告
- [LeetCode] Recover Binary Search Tree 解题报告
- [LeetCode] Pow(x, n) 解题报告
- [LeetCode] Remove Duplicates from Sorted Array 解题报告
- ANSYS SpaceClaim 2015中文免费版
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- [LeetCode] Remove Duplicates from Sorted List 解题报告
- [LeetCode] Remove Element 解题报告
- [LeetCode] Remove Nth Node From End of List 解题报告
- [LeetCode] Restore IP Addresses 解题报告
- [LeetCode] Reverse Integer 解题报告
- [LeetCode] Reverse Linked List II 解题报告