Description

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 = [,,],

Your function should return length = , with the first two elements of nums being  and  respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [,,,,,,,,,],

Your function should return length = , with the first five elements of nums being modified to , , , , and  respectively.

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

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = ; i < len; i++) {
print(nums[i]);
}

正确实现:

public class Solution {
public int RemoveDuplicates(int[] nums) {
if(nums.Length == )
return ;
int i,j = ;
for(i = ; i < nums.Length; i++){
if(nums[i] != nums[j])
nums[++j] = nums[i];
}
return j+;
}
}

我的疑惑:这样子实现会出现错误 数组越界 很是不解 但是后边再一次提交就通过了

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

LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑的更多相关文章

  1. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  2. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  3. LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...

  4. 【leetcode❤python】26. Remove Duplicates from Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def removeDuplicates(self, nums):        "&quo ...

  5. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

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

  7. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

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

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

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

随机推荐

  1. Swift——(六)Swift中的值类型

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/34855597     在Swift中,结构体和枚举 ...

  2. java面试题-看到那记录到哪

    哈希冲突 如果两个不同的元素,通过哈希函数得到的实际存储地址相同怎么办?也就是说,当我们对某个元素进行哈希运算,得到一个存储地址,然后要进行插入的时候,发现已经被其他元素占用了,其实这就是所谓的哈希冲 ...

  3. PHP include 与 require 区别

    include 与 require 语句同样用于向 PHP 代码中引用文件. include 与 require 有一个巨大的差异:  include 语句引用某个文件并且 PHP 无法找到它,脚本会 ...

  4. 【目录】sql server 性能调优

    随笔分类 - sql server 性能调优 sql server 性能调优之 资源等待之网络I/O 摘要: 一.概述 与网络I/O相关的等待的主要是ASYNC_NETWORK_IO,是指当sql s ...

  5. 【学习总结】Python-3-风格各异的数值类型实例

    菜鸟教程-Python3-基本数据类型 可能是考点的各种形态的数值类型 int型:正数负数,八进制0开头,十六进制0x开头 float型:小数点的前后都可以没有数字,自动补全 complex型:虚部的 ...

  6. JavaScript_基础笔记

    javaScript基础:概念:一门客户端脚本语言 运行在客户端浏览器中的,每一个浏览器都有javaScript的解析引擎 脚本语言:不需要编译,直接可以被浏览器解析执行功能区: 可以来增强用户和ht ...

  7. 转:谈谈Linux下动态库查找路径的问题

    http://blog.chinaunix.net/uid-23069658-id-4028681.html 学习到了一个阶段之后,就需要不断的总结.沉淀.清零,然后才能继续“上路”.回想起自己当年刚 ...

  8. Java集合框架是什么?说出一些集合框架的优点?

    每种编程语言中都有集合,最初的Java版本包含几种集合类:Vector.Stack.HashTable和Array. 随着集合的广泛使用,Java1.2提出了囊括所有集合接口.实现和算法的集合框架.在 ...

  9. 开发react 应用最好用的脚手架 create-react-app

    安装 npx create-react-app my-app cd my-app npm start 安装后之后,就是这样的了 配置 这样的”零配置”没法满足我们的需求,我们需要自定义,需要加一些 l ...

  10. 关于使用html2canvas 绘制图片的坑

    html2canvas绘制跨域图片之后,会导致画布被污染,从而无法使用canvas的toDateUrl()等方法获取图片数据的方法,这是canvas的限制而并非html2canvas的原因.好了锅甩好 ...