题目描述:

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 int removeDuplicates(int[] nums) {
int length = nums.length;
int count = 1;
if (length == 0)
return 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] == nums[i])
continue;
else {
nums[count] = nums[i];
count++;
}
}
return count;
}

最新文章

  1. C++随笔:.NET CoreCLR之GC探索(4)
  2. Java Spring的IoC和AOP的知识点速记
  3. OData的初步认识
  4. 装配bean
  5. js基本类型 引用类型
  6. win10开始菜单打不开怎么办 win菜单键没反应解决办法
  7. cas4.2.7实现单点登录
  8. SPFA【模板】单源最短路径
  9. Factom(公证通)--基于区块链的存证系统
  10. mysql在win10下的卸载
  11. OpenCV中feature2D——BFMatcher和FlannBasedMatcher
  12. 小程序2-基本架构讲解(一)JSON配置与详解
  13. 4-24日 collections模块 random模块 time模块 sys模块 os模块
  14. 【转】oracle中的游标的原理和使用详解
  15. js中引号(&quot;&quot;)中间设置变量
  16. 1: 介绍Prism5.0 Introduction to the Prism Library 5.0 for WPF(英汉对照版)
  17. 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】
  18. Zabbix——使用邮件报警
  19. 怎样将游戏从Unity导到iOS设备上
  20. WPF之ContextMenu的命定绑定

热门文章

  1. 虚拟机安装Centos6.5之后的网络配置
  2. C# 读取oracle 中文乱码的解决方案
  3. wpf datagrid 行双击事件
  4. poj 3415 Common Substrings 后缀数组+单调栈
  5. DNF技能贴图的研究
  6. ITaCS Change Password web part
  7. linux源码分析2
  8. Angular指令封装jQuery日期时间插件datetimepicker实现双向绑定
  9. 增强LSH
  10. [转载]C# 中Web.config文件的读取与写入