题目:

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.

翻译:

给定一个排序好的数组,就地移除反复的元素。来使得每一个元素仅仅出现一次,而且返回新的长度。

不要分配额外的空间给还有一个数组。你必须完毕它在原数组上。而且仅仅使用常数级的内存。

分析:

因为须要移除反复的元素,所以必须有移动元素的操作。当遍历的i指针指向的元素与上一个元素反复时。须要採用一个指针nextEmpty来记录当前这个位置(须要被移除的位置,也就是要把后面的元素复制过来的位置),当遍历到下一个不反复的元素时。再拷贝到这个位置。

代码:

public class Solution {
public int removeDuplicates(int[] nums) {
int nextEmpty=1;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]){
nums[nextEmpty]=nums[i];
nextEmpty++;
}
}
return nextEmpty;
}
}

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]的更多相关文章

  1. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  2. 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 ret ...

  3. LeetCode:26. Remove Duplicates from Sorted Array(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...

  4. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  5. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  6. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  7. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. 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 ...

  9. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

随机推荐

  1. 常用的几个Dos命令-持续更新中

    1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...

  2. 下载github项目

    两种方法:通过https或者ssh地址 找一个放置项目的文件夹,右键git bash here 输入 $ git clone https://项目地址 通过https 项目地址可以直接复制网页地址,或 ...

  3. Python操作远程数据库

    我的项目要往数据库中插入create_time和update_time,那就势必要引用现在的系统时间,经过大量的查找,终于发现往python是没有对应时间datetime的相关通配符的,那么我们要怎么 ...

  4. jboss之启动加载过程详解

    今天看了看jboss的boot.log和server.log日志,结合自己的理解和其他的资料,现对jboss的启动和加载过程做出如下总结: boot.xml是服务器的启动过程的日志,不涉及后续的操作过 ...

  5. 语音跟踪:信号分解、锁相、鸡尾酒会效应、基于PR的信号分离

    NLP中关于语音的部分,其中重要的一点是语音信号从背景噪音中分离.比如在一个办公室场景中,有白天的底噪-类似于白噪音的噪音.空调的声音.键盘的啪啪声.左手边45度7米元的地方同事讨论的声音.右手边1. ...

  6. 安卓app测试之cpu监控

    安卓app测试之cpu监控,如何获取监控的cpu数据呢? 一.通过Dumpsys 来取值 1.adb shell dumpsys cpuinfo 二.top 1.top -d 1|grep packa ...

  7. ionic3视频播放功能

    因为项目的需要,需要使用视频播放的功能,使用的是videogular2插件,但是报了一个无法识别video-player 这个标签,百度了很多,发现原来是版本 不对,ionic3是以来angular5 ...

  8. P1541 乌龟棋 题解(洛谷,动态规划递推)

    题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...

  9. 洛谷——P1850 换教室

    P1850 换教室 有 2n 节课程安排在 nn 个时间段上.在第 i个时间段上,两节内容相同的课程同时在不同的地点进行,其中,牛牛预先被安排在教室 $c_i$​ 上课,而另一节课程在教室 $d_i$ ...

  10. subprocess操作命令

    import subprocess 一. run()方法 --->括号里面传参数,主要有cmd, stdout, shell, encoding, check 1.直接传命令 2.命令带参数要以 ...