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. vue - blog开发学习5

    基本功能和后台联调 1.首页的所有博客 因为是前后台都是本地开发,所以前端vue需要设置proxy:修改/config/index.js中的这个proxyTable proxyTable: { '/a ...

  2. SqlServer表名称定义

    每一个数据表 添加一个 扩展 属性:Description  填写表描述. 查看是否所有表都添加的Sql如下: SELECT a.name AS name, g.[value] FROM sys.ta ...

  3. c# HttpListener 使用

    与 IIS 上发布网站相比,使用 HttpListener 编程的程序更加轻量化,易于发布和更新.配合 Thread 或 Task 类也可满足一定的并发. https://docs.microsoft ...

  4. alpha阶段绩效考核

    (按姓氏拼音顺序) (评分还考虑了从开题至今的博客.汇报等工作,但由于太杂乱没法列出) 陈修远 B+ 后端技术踩坑及代码编写 傅泳淦 A- Android端技术踩坑及代码编写 李浩冉 B   后端知识 ...

  5. 2018-10-8-如何安装-btsync

    title author date CreateTime categories 如何安装 btsync lindexi 2018-10-8 9:15:6 +0800 2018-2-13 17:23:3 ...

  6. go语言从例子开始之Example15.闭包

    Go 支持通过 闭包来使用 匿名函数.匿名函数在你想定义一个不需要命名的内联函数时是很实用的. 闭包简单理解,函数反回值是一个函数 Example: package main import " ...

  7. bootstrap.yml

    spring: jpa: properties: hibernate.enable_lazy_load_no_trans: true application: name: paycore cloud: ...

  8. 你了解SEO中的时效性吗?

    你了解SEO中的时效性吗? 本文摘自web前端早读课,侵删. 前言 最近刚好在负责一个新项目,App在还没上线的前提上,PC/WAP可以优先部署相关SEO,这样在后续的推广中得以运用.今日早读文章由腾 ...

  9. vue中filters(过滤器)的使用

    在vue中使用filters Vue.js自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式.过滤器应该被添加在 JavaScript 表达式的尾 ...

  10. I2C自编设备驱动设计

    一.自编设备驱动模型 at24.c: static int __init at24_init(void) { io_limit = rounddown_pow_of_two(io_limit); re ...