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. CXF自动生成客户端

    官网下载apache-cxf-3.1.6,bin目录下.配置环境变量,生成客户端 http://blog.csdn.net/jaune161/article/details/25499939 http ...

  2. tableView创建方法调用的研究

    当两个section的cell数量都为5的时候,方法的调用顺序: -[ViewController numberOfSectionsInTableView:] -[ViewController tab ...

  3. JavaScript--数组--sort比较器

    因为原装的sort这个API其实是先把要比较的数转换为字符串再进行比较的,所以并不好用 所以准备自定义一个比较器函数: //sort原理--->sort(arr,compare) functio ...

  4. dota监测

    漫漫长假一个人无聊得很,整日DOTA,打的腰酸背痛腿抽筋的.就想着写一个脚本记录自己每天打游戏的时间,于是就产生了下面的这个东西... 运行环境:win7 32位. python版本:3.4.1 由于 ...

  5. 【转】POJ题目分类

    初级:基本算法:枚举:1753 2965贪心:1328 2109 2586构造:3295模拟:1068 2632 1573 2993 2996 图:最短路径:1860 3259 1062 2253 1 ...

  6. 【jquery学习笔记】关于$(window),$("html,body").scroll()的在不同浏览器的不同反应

    已经很几次碰到了这种问题, 例子: $(window).scroll(function(){ var num=$(window).scrollTop();              //之前的写法是$ ...

  7. Python自动化运维之20、HTML

    一.HTML相关概念 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这 ...

  8. Android模拟器genymotion安装与eclipse 插件安装

    推荐一款Android模拟器"Genymotion",有点速度快,占用资源少,可整合eclipse.闲话少谈,看安装步骤. 1.下载地址:https://www.genymotio ...

  9. CentOS 7 安装tomcat

    1.下载Linux版的tomcat 2.上传下载tomcat文件到/usr/local中执行以下操作 [root@admin local]# cd /usr/local [root@admin loc ...

  10. spinner 下拉框控件

    spinnerMode=dropdown时,为下拉模式spinnerMode=dialog时,会在界面中间弹出Android:popupBackground=”#f0000000”,可以去除spinn ...