LeetCode 26 Remove Duplicates from Sorted Array
2016-03-23 22:41
495 查看
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 =
Your function should return length =
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
1and
2respectively.
It doesn't matter what you leave beyond the new length.
注意对数组边界的处理,代码如下:
public int removeDuplicates(int[] nums) { int count = 0; Arrays.sort(nums); int len = nums.length - 1; if (len <= 0) return len + 1; for (int i = 0; i < len; i++) { if (nums[i] != nums[i + 1]) { nums[count++] = nums[i]; } } nums[count++] = nums[len]; return count; }
相关文章推荐
- HDU 1097 A hard puzzle
- MyBatis的CRUD操作
- VC6.0 点击按钮添加ado数据到数据库总是失败
- jsp中的内置对象
- XHR2.0
- 折半插入排序
- Invalid project description. xxxx overlaps the location of another project: ‘xxx’
- UVA 1398 Meteor
- Theano 中 scan 函数的参数解释
- 深入了解java和javascript-杂谈章
- Nginx常用配置总结
- CF_4B_BeforeAnExam
- Tomcat 7.x下jspspy无法使用的解决方法
- 找出所有从根节点到叶子节点路径和等于n的路径并输出
- 《算法竞赛入门经典》5.32排序与检索-字母重排
- python 回调函数和回调方法的实现分析
- 求解算术表达式结合二叉树的后缀表达式问题
- AJAX POST&跨域 解决方案 - CORS
- 二叉树层次遍历
- 代码复审