26. Remove Duplicates from Sorted Array
2016-06-08 11:37
239 查看
题目:https://leetcode.com/problems/remove-duplicates-from-sorted-array/
代码:
代码:
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length==0) return 0; int new_length = nums.length; int i=0; while(i<new_length-1) { if(nums[i]==nums[i+1]) { new_length--; remove(nums,i); continue; } i++; } return new_length; } public void remove(int[] nums,int i) { for(int j=i;j<nums.length-1;j++) { nums[j] = nums[j+1]; } } } 91ms!!!beat 1.47% bad code!!!!!! =============================修改=============== public class Solution { public int removeDuplicates(int[] nums) { int i=0; for(int n:nums) { if(i<1||n>nums[i-1]) nums[i++] = n; } return i; } } 1ms
相关文章推荐
- React + Redux
- 怎么用EDIUS将静帧图片做出动态特效
- Android之SurfaceView学习(一)
- oracle ORA-28000: the account is locked
- Linux Shell系列教程之(八)Shell printf命令详解
- C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁
- Linux 设备驱动--- 内核等待队列
- JAVA中批量插入数据
- 带您认识不一样的Cognos
- plsql安装--无需安装oracle客户端
- WIN10下阿里WEEX环境搭建
- 为什么响应式编程并非一时之势?
- 函数
- 位运算符——左移(<<)、右移(>>)、与(&)、非(~)、或(|)、异或(^)
- HTML学习
- popuwindow弹出时,背景半透明
- 宏函数的注意事项
- NetUtils网络连接工具类
- QT 用 paintEvent 实现绘制钟表的列子
- scrollTop、offsetHeight和offsetTop等属性用法详解