Remove Duplicates from Sorted Array
2015-12-20 23:10
369 查看
package cn.edu.xidian.sselab.array; /** * * @author zhiyong wang * title:Remove Duplicates from Sorted Array * content: * 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. */ public class RemoveDuplicatesfromSortedArray { //这个题与RemoveElement几乎相同,因为是已经排好序的数组,比较条件改成相邻的两个数进行比较 //同样是统计double的数字的对数,每个数前移相同的数字的个数 //不过比较的时候要注意问题,不要越界,这里特别主要,nums里面个数小于2个时候,不做任何处理,只有多于1个时才会进行比较 public int removeDuplicates(int[] nums){ int length = nums.length; int count = 0; if(length>1){ for(int i=1;i<length;i++){ if(nums[i-1] == nums[i]){ count++; }else{ nums[i-count] = nums[i]; } } } return length-count; } } |
相关文章推荐
- CentOS的Linux操作系统图形界面开启
- Sql 工具的使用
- RabbitMQ部署流程
- 如何调用百度的api
- 服务器远程监控管理(二)-系统远程安装部署
- java垃圾回收
- 校OJ 11076 浮点数的分数表达
- EasyUI-----给combobox添加OnSelect事件
- VPS环境搭建详解(Virtualenv+Gunicorn+Supervisor+Nginx+Django+Flask+oneapm+event
- 第三十六课 用循环处理文字 【项目1-5】
- ACM 素数距离
- linux中结构体成员变量加“点”问题
- C#委托与C语言函数指针及函数指针数组
- 未能从程序集“System.ServiceModel, Version=3.0.0.0......解决方法
- Confluence 5.4实现与JIRA前所未有的集成
- 记一次驴唇不对马嘴的DIY之旅(二)
- Mapreduce部署与第三方依赖包管理
- 39节课后再解
- python string 常用方法
- 递归查看某个文件夹的所有子文件