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. 小程序通过background-image设置背景图片

    微信小程序通过background-image设置背景:只支持线上图片和base64图片,不支持本地图片:base64图片设置步骤如下: 1.在网站http://imgbase64.duoshiton ...

  2. 装系统时 System clock uses UTC 问题

    装系统也装了至少不下50次了,每次都是傻瓜一样的按照第一印象在弄,从未想过为啥,装到这里的时候,System clock uses UTC 勾不勾呢,每次都是百度,然后装完这一次下一次又忘了,这是没有 ...

  3. 2.3 Visio画虚线后插入word或PPT变为实线

    选中实线后,左键选择->格式->线条->粗细->自定义->设置为0pt

  4. Hbase rowkey设计+布隆过滤器+STORE FILE & HFILE结构

    Rowkey设计 Rowkey设计原则 Rowkey设计应遵循以下原则: 1.Rowkey的唯一原则 必须在设计上保证其唯一性.由于在HBase中数据存储是Key-Value形式,若HBase中同一表 ...

  5. 剑指offer 1.数组 二维数组中查找

    题目描述 在一个二维数组中(每个一维数组的长度相同), 每一行都按照从左到右递增的顺序排序, 每一列都按照从上到下递增的顺序排序. 请完成一个函数, 输入这样的一个二维数组和一个整数,判断数组中是否含 ...

  6. Excel技巧--反向查询

    当要从左侧的表格,查询某人所在的部门时,那么需要逆向查询.VLOOKUP函数只能正向查询.可以使用Match和index函数: Match函数:查询某个值在指定区域所在的位置: Index函数:查询指 ...

  7. Excel技巧--做一去重复的数据下拉列表

    当我们有一数据列表(内含重复数据),将该数据做成如下图的下拉列表: 可以这样做: 1.选中该标题行,按ctrl+shift+下方向键,将该列有数据的区域选中: 2.点击“数据”—>删除重复项: ...

  8. Java8-dateTimeFormatter

    时间格式化LocalDate,DateTimeFormatter--->parse,ofParttern 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的日期时 ...

  9. Ubuntu 14.10 下DokuWiki安装

    环境说明: Ubuntu 14.10 64位 1 下载DokuWiki:http://download.dokuwiki.org/ 2 解压到 /var/www/html下面 3 如果没有安装Apac ...

  10. .net mvc 分页

    1.分页实体类 public class PageDto { public int PageIndex { get; set; } public int PageSize { get; set; } ...