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. Labview 机器视觉IMAQ GetFileInfo函数详解

    ------------恢复内容开始------------ IMAQ GetFileInfo作用是获取图片文件的信息,包括Calibration(校准).文件类型.水平垂直分辨率.文件数据类型.图像 ...

  2. Jmeter连接mysql数据库?so easy!!!

    一.确保mysql数据库能够通过Navicat等远程连接工具连接. 注意:一定是确保能使用navicat连接,而不是dos窗口! 比如笔者需要查询ecshop数据库下的ecs_admin_user表, ...

  3. CTF中的命令执行绕过

    本位原创作者:Smity 在介绍命令注入之前,有一点需要注意:命令注入与远程代码执行不同.他们的区别在于,远程代码执行实际上是调用服务器网站代码进行执行,而命令注入则是调用操作系统命令进行执行. 作为 ...

  4. PHP代码审计理解(三)---EMLOG某插件文件写入

    此漏洞存在于emlog下的某个插件---友言社会化评论1.3. 我们可以看到, uyan.php 文件在判断权限之前就可以接收uid参数.并且uid未被安全过滤即写入到了$uyan_code中. 我们 ...

  5. [git] github 推送以及冲突的解决,以及一些命令

    推送以及冲突的解决:(我的觉得先看完) (正常情况就是把修改的文件 git add 然后git commit 然后推送就行啦): 下面是一些命令 1.查看分支状态(查看所有:当前检出分支的前面会有星号 ...

  6. 设计模式 - 命令模式详解及其在JdbcTemplate中的应用

    基本介绍 在软件设计中,我们经常需要向某些对象发送一些请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需要在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来设计,使得 ...

  7. Oracle Database 12C 安装教程

    一.下载地址 Oracle Database 官方下载地址:https://www.oracle.com/database/technologies/oracle-database-software- ...

  8. iOS GIF图片转UIImage

    多平台保持统一风格的UI设计,少不了一些动态图片的使用 1.本地GIF图片使用 1.1 将本地GIF转为NSdata类型 NSData *tempdata = [NSData dataWithCont ...

  9. 双系统情况下,ubuntu开机挂载Windows分区

    首先:blkid,查看分区所属uuid 其中 /dev/sda5 就是Windows分区 其次:fdisk -l,查看分区情况 通过硬盘大小找到对应要设置的具体分区(其实这步也不用,我只是为了确定) ...

  10. openssl 查看证书

    查看证书 # 查看KEY信息 > openssl rsa -noout -text -in myserver.key # 查看CSR信息 > openssl req -noout -tex ...