[C++]LeetCode: 2 Remove Duplicates from Sorted Array
来源:互联网 发布:掌控网络 编辑:程序博客网 时间:2024/05/02 01:00
题目:
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]
.
思路:只需要把每个不重复数字按顺序存储到一起即可;设置两个下标i,j,使用j遍历数组, i是标记,标记每段重复的开始数字,后面的数字也是和A[i]比较;j是把尺子在数组内移动。
Attention: 注意思考没有重复数字的特殊情况。
AC code:
class Solution {public: int removeDuplicates(int A[], int n) { //只需要把每个不重复数字按顺序存储到一起即可 if(n == 0) return 0; //i是标记,标记每段重复的开始数字,后面的数字也是和A[i]比较;j是把尺子在数组内移动 int i = 0, j = 1; while(j < n) { if(A[j] == A[i]) { j++; } else { A[++i] = A[j++]; } } return i + 1; } };
0 0
- [C++]LeetCode: 2 Remove Duplicates from Sorted Array
- LeetCode -- Remove Duplicates From Sorted Array 2
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- [C++]LeetCode: 72 Remove Duplicates from Sorted Array II
- [LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)
- [leetcode](Remove Duplicates from Sorted Array II C语言实现)
- 【leetcode c++】26 Remove Duplicates from Sorted Array
- 【leetcode c++】80 Remove Duplicates from Sorted Array II
- [leetcode-26]Remove Duplicates from Sorted Array(C)
- [leetcode-80]Remove Duplicates from Sorted Array II(C)
- [C语言][LeetCode][26]Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array C语言
- leetcode刷题系列C++-Remove Duplicates From Sorted Array
- 【LeetCode-26】 Remove Duplicates from Sorted Array(C++)
- [C语言][LeetCode][80]Remove Duplicates from Sorted Array II
- 【C++】【LeetCode】26. Remove Duplicates from Sorted Array
- leetcode 26:Remove Duplicates from Sorted Array (C)
- 【转载】做一名开源社区的扫地僧(上)
- C++中的返回值引用
- [LINUX内核编程]学习笔记(二)
- HTML+JavaScript实现链式运动特效
- @Temporal
- [C++]LeetCode: 2 Remove Duplicates from Sorted Array
- (1)求a+b
- Uploadify404无效链接
- Android Bitmap保存到SQLite
- 答读者问(24):一个大二学生有关数据结构学习的疑问及答复
- 字符串循环右移n个字符
- 高精度模板之高精乘高精
- JavaScript正则表达式语法及使用范例大全
- 【SGU】105. Div 3 水题