26. Remove Duplicates from Sorted Array (Easy)

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

Given 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 returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

solution##

解法一

class Solution {
public int removeDuplicates(int[] nums) {
int newlength = 0, k = 0;
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] < nums[i+1]) //如果两数不同
{
k = i + 1; //记录不同两数中下一个的下标
newlength++; //新数组长度++
if (k > newlength) //如果k与newlength不同步,说明有重复的数
nums[newlength] = nums[k]; 将nums[k]移动到newlength处
}
}
return newlength + 1; //此处不能用newlength++
}
}

解法二

class Solution {
public int removeDuplicates(int[] nums) {
int newlength = 0;
for (int i = 0; i < nums.length; i++)
{
if (nums[newlength] != nums[i])
nums[++newlength] = nums[i];
}
return newlength+1; //此处不能用newlength++
}
}

总结##

题意是给定一个排好序的数组,需要将里面不重复的数字按顺序放在数组左端,构成一个新的数组,且这些不重复数字的长度为length,超过length长度的数字不用理会。

第一种解法没第二种解法简单,故只描述第二种解法。第二种解法的思路是设置一个计数器newlength,初始值为0,然后遍历数组,如果nums[newlength] != nums[i],则将nums[++newlength]置为nums[i],否则什么都不做;遍历完成后返回newlength+1;

Notes

1.注意++i与i++的区别;

LeetCode--Array--Remove Duplicates from Sorted Array (Easy)的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. 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++> 给出排序好的 ...

  3. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

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

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  10. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. 原创Pig0.16.0安装搭建

    tar -zxvf pig-0.16.0.tar.gz -C ~   vi ~/.bash_profile export PIG_HOME=/home/hadoop/pig-0.16.0 export ...

  2. kafka消息分区机制原理

    背景 kafka如何支撑海量消息的集中写入? 答案就是消息分区. 核心思想是:负载均衡,采用合适的分区策略把消息写到不同的broker上的分区中: 其它的产品中有类似的思想. 比如monogodb, ...

  3. SQL Server 之T-SQL基本语句 (3)

    继续来用例子总结sql基本语句用法. 在这里在建一个表:课 课程名 上课时间 数学 周一 数学 周二 数学 周三 语文 周一 语文 周二 英语 周一 数据分组:GROUP  BY select  课程 ...

  4. ASP.NET母版页

    ASP.NET母版页:主要是设置一致界面的页面,在固定的页中进行更新. 如图1-1所示 页头 页中(页内容) 页尾 图1-1  母版页 一般网页是固定页头和页尾,只更新页内容,来实现网页的跳转或内容的 ...

  5. Mysql列属性

    列属性又称之为字段属性在mysql中一共有6个属性:null,默认值(default),列描述(comment),主键(primary key),唯一键(unique key)和自动增长 修改数据库字 ...

  6. pytorch seq2seq闲聊机器人加入attention机制

    attention.py """ 实现attention """ import torch import torch.nn as nn im ...

  7. 关于go的通信通道channel——chan的一些问题

    go版本 1.8 chan类型的声明,有以下几种: var c chan int c := make(chan int) //slice.map.chan都可以通过用make来初始化,其中map.ch ...

  8. 前端基础进阶(七)-前端工程师最容易出错的问题-this关键字

    我们在学习JavaScript的时候,因为对一些概念不是很清楚,但是又会通过一些简洁的方式把它给记下来,那么这样自己记下来的概念和真正的概念产生了很强的偏差. 当然,还有一些以为这个是对的,还会把它发 ...

  9. h5前端animate等js特效问题汇总

    1.jq中的animate不要重复大量使用,会导致内存溢出或泄漏,很直观的现象就是手机发热太严重: 2.input 的button类型的去掉默认样式并换颜色: -webkit-appearance:n ...

  10. java学习(第五篇)包装类

    一.Integer package com.test01; public class IntegerTest01 { public static void main(String[] args) { ...