LEETCODE: Remove Duplicates from Sorted Array
来源:互联网 发布:明源软件上海分公司 编辑:程序博客网 时间:2024/05/17 02:37
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],
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].
放在以前,我做这道题目也是很混乱的。现在一看,就是从后往前便利,如果有重复,算出新的长度,并且根据减少的元素个数调整当前位置后面的元素。
class Solution {public: int removeDuplicates(int A[], int n) { if(A == NULL || n == 0) return 0; int newLength = n; for(int ii = n - 1; ii > 0; ii --) { int jj = ii; while(A[jj] == A[jj - 1] && jj > 0) { jj --; newLength --; } if(jj != ii) { for(int kk = jj + 1; kk < newLength; kk ++) { A[kk] = A[kk + (ii - jj)]; }ii = jj; } } return newLength; }};
0 0
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】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 II
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- leetcode 35: 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] 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
- [leetcode]Remove Duplicates from Sorted Array II
- 【Java.NIO】NIO就绪处理之OP_WRITE
- UVAOJ--10718 Bit Mask
- HADOOP Reading List
- ubuntu14.10 安装最新中文firefox
- 英文写作笔记--转自知乎
- LEETCODE: Remove Duplicates from Sorted Array
- windows上安装 Twisted
- Android 控件在布局中按比例放置
- UML:九种图的学习
- Linked List Cycle && Linked List Cycle II 解体思路
- 黑马程序员:论反射的正确姿势以及应用范围
- 计算机视觉领域的网站
- bootstrap实现面板中展示内容
- HDOJ 4608 I-number