LeetCode(26) Remove Duplicates from Sorted Array
2016-01-19 08:47
281 查看
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 nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond 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 nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
public int removeDuplicates(int[] nums) { int index = 0; if(nums.length == 0) return 0; for(int i = 1; i < nums.length; i++){ if(nums[index] != nums[i]) nums[++index] = nums[i]; } return index+1; }
相关文章推荐
- 哪个文件系统最适合你的 Linux 系统?
- Oracle性能优化之COUNT
- 开发与研发
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- iOS应用中如何声明全局变量?
- 我不强求你做快乐的孩子——心理咨询师父亲给女儿的公开信
- 如何下载谷歌/百度/高德大字体地图用于打印
- 从零开始搭建Hadoop2.7.1的分布式集群
- mysql 语句练习
- JavaScrip——简单练习(抓错误信息,for循环,日期)
- Codeforces Good Bye 2015 (A,B,C,D)
- 如何下载无偏移的谷歌电子/卫星地图
- PHP得到公网IP和所在城市的代码
- sqlite3数据库基本使用
- 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>
- hadoop 10篇博客
- asp.net实现大文件上传
- bzoj1570: [JSOI2008]Blue Mary的旅行
- CharsetDecoder.decode()与Charset.decode()
- 单例模式