题目描述:Remove Duplicates from Sorted Array

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

Java:

    public int removeDuplicates(int[] nums) {

        if (nums.length < 2) {
return nums.length;
} int i = 1;
int j = 0; while (i < nums.length) {
if (nums[i] == nums[j]) {
i++;
} else {
j++;
nums[j] = nums[i];
i++;
}
} return j + 1;
}

最新文章

  1. JQuery Easy Ui 可装载组合框 - ComboBox
  2. CF #305(Div.2) D. Mike and Feet(数学推导)
  3. android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
  4. Yii2.0 查询(CRUD)
  5. CAShapeLayer 与贝塞尔曲线
  6. 对于C语言中数组名是指针的理解
  7. vs 2013 编译zlib
  8. 【物联网云端对接-2】通过MQTT协议与阿里云物联网套件进行云端通信
  9. 简易js进度条
  10. MIP技术进展月报第3期:MIP小姐姐听说,你想改改MIP官网?
  11. [算法&amp;数据结构]深度优先搜索(Depth First Search)
  12. node 和 npm 常用命令
  13. 【资料下载区】【GK101固件】更新日期2017/1/11
  14. [Java]判断Integer值相等最好不用==最好使用equals
  15. ethereumjs/ethereumjs-wallet
  16. Django中组合搜索功能
  17. JMeter基础:请求参数Parameters 、Body Data的区别
  18. 面向连接的传输TCP(一)
  19. 多种移动平均计算总结(MA,EMA,SMA,DMA,TMA,WMA)
  20. DataTable Excel 导出:

热门文章

  1. 微信小程序简单封装图片上传组件
  2. UOJ Round总结
  3. C++ 有用的资源
  4. python爬虫构建代理ip池抓取数据库的示例代码
  5. Python基本数据类型与数据结构(数据挖掘学习)
  6. 剑指Offer-Python(21-25)
  7. .NET5都来了,你还不知道怎么部署到linux?最全部署方案,总有一款适合你
  8. 面经手册 &#183; 第18篇《AQS 共享锁,Semaphore、CountDownLatch,听说数据库连接池可以用到!》
  9. ceph luminous bluestore热插拔实现
  10. Ubuntu12.10 设置默认命令行启动