(Java)LeetCode-26. Remove Duplicates from Sorted Array
2016-06-08 15:08
597 查看
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.
这道题蛮简单的,用两个数组,一个指针指向前面即将要被替换的数,一个指针遍历数组找到重复的即可。这题只是个铺垫而已~
public class Solution {
public int removeDuplicates(int[] nums) {
int changed = 1;
int n = 1;
while( n < nums.length){
if( nums
!= nums[changed-1]){
nums[changed] = nums
;
changed++;
}
n++;
}
return changed;
}
}
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 class Solution {
public int removeDuplicates(int[] nums) {
int changed = 1;
int n = 1;
while( n < nums.length){
if( nums
!= nums[changed-1]){
nums[changed] = nums
;
changed++;
}
n++;
}
return changed;
}
}
相关文章推荐
- 13 java.io.*包 和输入、输出类构成。
- java 通过url下载文件到浏览
- 优秀JAVA第三方库
- 关于java的一点成长
- spring mvc DispatcherServlet详解之一---处理请求深入解析
- Eclipse自动补全编辑时出现闪退
- Spring Annotation Processing: How It Works--转
- 转:java服务器安全指南
- java编码之BASE64
- 关于屏蔽eclipse相关警告的设置(android,java)
- 2016-06-08csdn博客
- 11 java.awt.* 包 组件
- 最近用CXF webservice做了服务端
- Spring 使用注解方式进行事务管理
- java正则表达式四种常用的处理方式(匹配、分割、替代、获取)
- Eclipse版本号的区别
- 使用Intellij idea2016 配置springmvc项目环境
- mac版netbeans修改jdk的版本
- java实现二叉树的构建以及3种遍历方法
- SpringMVC 访问静态资源的三种方式