1 Star 0 Fork 8

一切缘于你 / leetcode

forked from 程序员二师兄 / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LeetCode_26.java 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
程序员二师兄 提交于 2019-09-19 17:56 . Initial commit
/**
* @创建人 luoxiangs
* @创建时间 2019/9/19 17:08
* @描述 26. 删除排序数组中的重复项 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
*/
public class LeetCode_26 {
public static void main(String[] args) {
LeetCode_26 code26 = new LeetCode_26();
int[] nums = {1, 1, 2};
int[] nums2 = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
System.out.println(code26.removeDuplicates(nums));
System.out.println(code26.removeDuplicates(nums2));
}
/**
* Created by LuoXiang on 2019/09/19 17:31
* Desc: 快慢指针思路。慢指针只有不相等的时候才向前走一步,快指针每次都走一步
* 复杂度: 时间复杂度:O(N) ; 空间复杂度: O(1)
**/
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int slow = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[slow] != nums[i]) {
nums[++slow] = nums[i];
}
}
return slow + 1;
}
}
1
https://gitee.com/song574482856/leetcode.git
git@gitee.com:song574482856/leetcode.git
song574482856
leetcode
leetcode
master

搜索帮助