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:

  1. Given nums = [1,1,2],
  2.  
  3. Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
  4.  
  5. It doesn't matter what you leave beyond the returned length.

Example 2:

  1. Given nums = [0,0,1,1,1,2,2,3,3,4],
  2.  
  3. Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
  4.  
  5. It doesn't matter what values are set beyond the returned length.

题意:

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

思路:

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

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

指针i来遍历原数组。

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

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

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

代码:

  1. class Solution {
  2. public int removeDuplicates(int[] nums) {
  3. if(nums.length == 0) return 0;
  4. int j = 1;
  5. for(int i = 1; i < nums.length; i++){
  6. if(nums[i] != nums[j-1]){
  7. nums[j] = nums[i];
  8. j++;
  9. }
  10. }
  11. return j;
  12. }
  13. }

[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. IIS 集成模式 导致 AjaxPro 无法正常运行

    web.config 配置如下: system.web/httphandlers <httpHandlers> <add verb="POST,GET" path ...

  2. 给大厨写的R数据分析代码

    ###************************************** 新老客户统计 ***************************************### dachu &l ...

  3. Custom Grid Columns - FireMonkey Guide

    原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...

  4. 第一天Python

    一.开发语言 高级语言:Python  Java.PHP     高级语言--字节码(PHP适用于写网页) 低级语言:C.汇编--机器码(底层开发,根本,效率低) 二.Python种类 三.安装

  5. 浏览器调试动态js脚本

    前两天拉取公司前端代码修改,发现在开发者工具的sources选项里边,居然没有列出来我要调试的js脚本,后来观察了一下,脚本是动态在页面里引入的,可能是因为这样所以不显示出来,但是如果不能断点调试,只 ...

  6. oracle批量删除某个用户下的所有表

    打开sql developer,输入如下语句,把USERNAME替换为需要删除的的用户名 然后把查询出来的结果复制出来执行一遍就行了. SELECT 'DROP table '||table_name ...

  7. 阿里四不像Fourinone

    阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194

  8. python开发计算器

    1 业务需求 1.1 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16- ...

  9. redis5.0.4 集群搭建

    准备工作用两台虚拟机模拟6个节点,一台机器3个节点,创建出3 master.3 salve 环境. redis 采用 redis-5.0.4 版本. 两台虚拟机都是 CentOS ,一台 CentOS ...

  10. Numpy学习笔记(二)

    (1)NumPy - 切片和索引 l  ndarray对象中的元素遵循基于零的索引. 有三种可用的索引方法类型: 字段访问,基本切片和高级索引. l  基本切片 Python 中基本切片概念到 n 维 ...