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.

思路:此题比較简单。大意是将数组中反复点删除,然后返回新数组的长度。数组中前n个数就是新的数组。唯一难点是不能用额外空间。

具体代码例如以下:

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <= 1){
return nums.length;
}
int len = 1;//新的长度,至少为1,下面循环从i=1開始
for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[i-1]){//不等于前一个元素。长度+1
nums[len++] = nums[i];//将新的元素装到前len个
}
}
return len;
}
}

leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法的更多相关文章

  1. [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 ...

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

  3. [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 ...

  4. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

    一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...

  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 ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

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

  8. Leetcode 26. Remove Duplicates from Sorted Array (easy)

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

  9. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

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

随机推荐

  1. 《InsideUE4》UObject(三)类型系统设定和结构

    垃圾分类,从我做起! 引言 上篇我们谈到了为何设计一个Object系统要从类型系统开始做起,并探讨了C#的实现,以及C++中各种方案的对比,最后得到的结论是UE采用UHT的方式搜集并生成反射所需代码. ...

  2. 安卓之cannot convert from Fragment1 to Fragment

    在写一个音乐播放器的时候,用到了fragment,结果在需要返回Fragment的方法里面,无法将Fragment1(Fragment的子类)强制转换成Fragment, 很是纳闷,我是参照一个开源代 ...

  3. Android学习----五大布局

    1.LinearLayout 线性布局 android:orientation="horizontal" 制定线性布局的排列方式 水平 horizontal 垂直 vertical ...

  4. js 判断url的?后参数是否包含某个字符串

    function GetQueryString(name){    var reg=eval("/"+name+"/g");   var r = window. ...

  5. Apache Commons DbUtils Problem

  6. 1 Two Sum(找和为target的两个数字下标Medium)

    题目意思:给一个数组,找到和为target的两个元素的序号,并且只有一组这样的元素 思路:map<int,int>(nums[i],i+1),然后从后往前循环,用count找,比较i+1 ...

  7. codeforces 235 B. Let's Play Osu!

    You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. F ...

  8. 在Xcode中如何屏蔽某个源文件的编译警告信息

    某些时候如果我们的源码在编译过程中出现大量的编译警告时,看起来是挺不爽的:但又确实没办法解决警告问题的时候,我们可以使用下面的方法来屏蔽指定的某个文件的所有警告信息. 1.在Xcode中选中工程文件. ...

  9. OpenMP 并行化处理测试

    OpenMP 并行化处理测试 #pragma omp parallel for 这条语句是用来指定后面的for循环语句变成并行执行的,将for循环里的语句变成并行执行后效率会不会提高呢?还是测试一 下 ...

  10. Seek the Name, Seek the Fame

    poj2752:http://poj.org/problem?id=2752 题意:给你一个串,让你求前n个字符和后n个字符相同的n有多少,从小到大输出来. 题解:这一题要深刻理解KMP的next数组 ...