Remove Duplicates from Sorted Array
来源:互联网 发布:淘宝阿里云 编辑:程序博客网 时间:2024/05/18 00:52
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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]
.
题解:
双指针思想。并且保存前面遍历的元素的值。用以判断是否将新遍历的元素加入到数组中。
Code:
class Solution {public: int removeDuplicates(int A[], int n){int preElement=0;int pCur=0;for(int i=0;i<n;i++){if(i==0){preElement=A[i];A[pCur++]=A[i];}else{if(preElement!=A[i]){A[pCur++]=A[i];preElement=A[i];}}}return pCur;}};
0 0
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 大多企业仍然容易收到【DNS缓存中毒】攻击
- objective-c宏定义
- OpenGL: OpenGL下的alpha融合
- Ubuntu - JVM环境下中文不显示. 解决
- Eclipse常用快捷键(转载)
- Remove Duplicates from Sorted Array
- 【zxshuba散文】 我们要让生命更美丽
- 跟对方回复该交话费国际公法哈市的非官方的观点
- css 前端设计师必知的background属性(有CSS3内容)
- 的家伙房管局股份将覆盖后发的的故事的风格的风格的风格地方
- js判断浏览器类型及版本
- 的法国恢复的故事的分公司的方式的方式的范德萨发的说法
- 鬼地方个回复广东省分公司的风格的身份的师傅的说法
- Windows上VS使用ADO远程访问PostgreSQL