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 ...
随机推荐
- mac版VMware fusion
百度网盘链接:链接: https://pan.baidu.com/s/1o8BAsrg 安装教程网上很多的,首先要下载一个window 10或其他版本的iso镜像文件,然后很好安装的.
- c++中小项堆声明和使用【转】
c++默认是大顶堆,小顶堆有两种声明方法: 1.对于基本类型直接用 priority_queue<int, vector<int>, greater<int> >p ...
- autoconfig操作小结
1.添加maven配置在pom文件中 <profile> <id>prod</id> <properties> < ...
- Strusts2--课程笔记8
文件的和上传和下载: (1)文件的上传: Struts是通过拦截器实现文件上传的,而默认拦截器栈中包含了文件上传拦截器,故表单通过Struts2可直接将文件上传,其底层是通过apache的common ...
- 解题报告8VC Venture Cup 2017 - Elimination Round
题目链接:http://codeforces.com/contest/755 本蒟蒻做了半天只会做前两道题.. A. PolandBall and Hypothesis 题意:给出n,让你找出一个m, ...
- 在UE4中使用SVN作为source control工具
==========预先处理 1.到这个目录下 2.鼠标在空白处 按住shift键 同时右键 会多出一个 可以打开的cmd 3.输入命令,修改红线部分. me: 登陆svn地址的用户名, URL网址: ...
- 写了一下午的dijkstra。突然发现我写的根本不是dijkstra。。。。是没优化过的BFS.......
写了一下午的dijkstra.突然发现我写的根本不是dijkstra....是没优化过的BFS.......
- 2016腾讯"创益24小时"互联网公益创新大赛总结
上周末参加了腾讯的"创益24小时"互联网公益大赛,和两个小伙伴(设计师Beryl和产品经理Benny)浴血奋战两天一夜,完成了一个叫"彩虹桥"的公益项目. (一 ...
- Traffic Ccontrol(流量控制)
linux有一个成熟的带宽供给系统,称为Traffic Control(流量控制).这个系统支持各种方式进行分类.排序.共享和限制出入流量. 一.基础知识 让ip显示我们的链路 ip link li ...
- Linux中kettle自动化部署脚本
自己写的一个自动化在Linux中部署kettle的脚本,包括一些遇到的问题在脚本中都有涉及. kettle是官网最新版本pdi-ce-6.1.0.1-196.zip 目前最新版本下载地址:https: ...