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.

题意:

去除有序数组的重复元素,使得每个元素只能在数组中出现一次

思路:

仍然是在尽量不开新空间的情况下,

直接在原数组中进行操作。

指针i来遍历原数组。

指针j 从 index 1 开始, 因为index 为 0 的元素是什么牛鬼蛇神都不重要。

指针j 接受指针i扫过的、符合条件的元素。

指针j所到之处,便是新“数组”新生之时。

代码:

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

[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(删除数组反复点) 解题思路和方法

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

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

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

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

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

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

  6. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  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

    Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  9. Java [leetcode 26]Remove Duplicates from Sorted Array

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

随机推荐

  1. OpenLDAP一登录系统就修改密码

    1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepath /usr/lib/openldap modulepath /usr/lib64/openldap   ...

  2. 如何在myeclipse中实现jquery的自动提示功能

    在web开发过程中,myeclipse中jsp可以实现自动提示功能,但是jquery代码却无法实现自动提示,需要自己一个个手动去输入,效率过低,怎么办? 工具/原料   jquery 1.8.3.js ...

  3. putty安装和使用

    https://blog.csdn.net/l707941510/article/details/80520790

  4. 记一次sql server 2005访问http接口,并解析json的过程

    记一次sql server 2005访问http接口,并解析json的过程  JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...

  5. 使用nrm工具高效地管理npm源

    在使用npm时,官方的源下载npm包会比较慢,国内我们基本使用淘宝的源,如果公司内部搭建了一套npm私有仓库,公司内部的源不可能把npm官方的npm包都同步,所以需要切换npm源.如果使用npm/cn ...

  6. 学习笔记之Fluent Python

    Fluent Python by Luciano Ramalho https://learning.oreilly.com/library/view/fluent-python/97814919462 ...

  7. android AES 加密解密

    import java.security.Provider; import java.security.SecureRandom; import javax.crypto.Cipher; import ...

  8. .net core 中间件实战

    1.新建一个ASP.NET Core Web Application项目,选择空模板. 2.新建一个类RequestIPMiddleware.cs using Microsoft.AspNetCore ...

  9. ffmpeg 编译

    下载FFmpeg git clone https://git.ffmpeg.org/ffmpeg.git 配置编译FFmpeg ./configure --prefix=host --enable-s ...

  10. Java的synchronized的同步代码块和同步方法的区别

    synchronized同步方法和同步代码块的区别 同步方法默认使用this或者当前类做为锁. 同步代码块可以选择以什么来加锁,比同步方法更精确,我们可以选择只有会在同步发生同步问题的代码加锁,而并不 ...