LeetCode OJ 26. 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 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.
Subscribe to see which companies asked this question
【思路】
1. 举个例子[1,1,1,2,2,2,3,4,5],我的想法就是找到每一节重复数字的长度,然后把后面的数字向前移动,直到遍历到数组最后。
上述例子中,我们从头开始发现有1重复出现了3次,因此我们把1后面的数字向前移动2,变为[1,2,2,2,3,4,5],然后把数组的len变为len-2,重复上面的结果直到遍历到最后。但是这个方法的效率并不高,每发现一个重复的元素都要把后面的数字向前移动,有没有更好的思路呢?
2. 一个更好的方法是:我们维持两个变量,i 用来遍历数组,j 用来指示数组中不重复的那部分的最后一个值的下标。在遍历数组的过程中,如果当前值和前一个值不同,则nums[++j] = nums[i],否则的话继续向前遍历。形象化的过程如下:
- j = 0; i = 1;

- nums[i] 等于 nums[i-1];

- nums[i] 不等于 nums[i-1]; nums[++j] = nums[i];

- 省略若干步



【java代码1】
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0) return 0;
int len = nums.length;
int duplen = 0;
for(int i = 0; i < len - 1; i++){
duplen = 0;
for(int j = i + 1; j < len; j++){
if(nums[j] == nums[i]) duplen++;
else break;
}
if(duplen > 0){
for(int k = i + duplen + 1; k < len; k++){
nums[k-duplen] = nums[k];
}
len = len - duplen;
}
}
return len;
}
}
【java代码2】
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0)
return 0;
int j = 0;
for(int i=1; i<nums.length; i++) {
if (nums[i-1] != nums[i])
nums[++j] = nums[i];
}
return j;
}
}
LeetCode OJ 26. Remove Duplicates from Sorted Array的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 用两个栈实现队列,剑指offer P59
public class QueueByStack { private Stack<Integer> stack1; private Stack<Integer> stack2 ...
- 二十七、oracle 例外
一.例外分类oracle将例外分为预定义例外.非预定义例外和自定义例外三种.1).预定义例外用于处理常见的oracle错误.2).非预定义例外用于处理预定义例外不能处理的例外.3).自定义例外用于处理 ...
- proc中tran的一般处理
存储过程事物的处理 一般情况在proc中判断的处理我们都采用 ) begin select '错误' return end 而在tran中我们最好用抛异常的方式 begin try begin SET ...
- cocos2d-3.x 创建动画
1.多文件帧序列动画 TrademarkAnimation.h #ifndef __TRADEMARK_ANIMATION_H__ #define __TRADEMARK_ANIMATION_H__ ...
- HomeBrew 安装 PHP7.1(开发笔记)
HomeBrew 安装 PHP7.1 $ brew update $ brew tap homebrew/dupes $ brew tap homebrew/php $ brew update $ b ...
- img的问题
一个div的宽高比和 里面的img的宽高比是一样的 ,div img { width:100%:height:100%;} img {border:0} img{ 设置为border:none无 ...
- 手机浏览器无法获取COOKIE的原因
手机浏览器上无法使用cookie,肯能是 1. 浏览器禁用 COOKIE ,这个简单开启即可. 2. 可能是手机所在时区有问题,将COOKIE有效期设置更长时间测试下,在更改时区
- 纯 CSS 实现三角形尖角箭头的实例
上次无意中发现了个使用纯 CSS 实现三角形尖角箭头的方法 http://blog.csdn.net/zhouzme/article/details/18901943 ,但没有怎么用上,也没有详细完整 ...
- Debug / Inspect WebSocket traffic with Fiddler【转】
Introduction I have recently written a project using SignalR, which supports HTML 5 WebSocket. Howe ...
- 《JS权威指南学习总结--第7章 数组》
内容要点: 一. JS数组是无类型的:数组元素可以是任意对象,并且同一个数组中的不同元素也可能有不同的类型.数组的元素甚至也可能是对象或其他属性,这允许创建复制的数据结构,如对象的数组和数组的数组. ...