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. 数组归一 reduce (数组归一) reduceRight(从右至左)

    var addTwoNumbers = function(l1, l2) { var e = l1 .reverse() .map((v, index, array) => v * Math.p ...

  2. spring 事物(一)—— 事物详解

    事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 事务的特性 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么 ...

  3. MD5文件去重

    //计算文件的MD5码 private string getMD5Hash(string pathName) { string strResult = ""; string str ...

  4. Bootstrap 过渡效果

    <!DOCTYPE html> <html <!DOCTYPE html> <html lang="en"> <head> & ...

  5. 记录下通过Java代码打开cmd启动appium server及在使用过程中碰到的问题

    1.appium server启动后,执行测试脚本,appium日志报错,提示appium setting未安装(原因是小米手机在用appium desktop调试时总是提示是否安装appium se ...

  6. sass-loader屡次安装不生效的问题

    报错信息: npm WARN sass-loader@8.0.0 requires a peer of webpack@^4.36.0 but none is installed. You must ...

  7. Window01

    1 <script src="~/jquery-easyui-1.5.5.2/jquery.min.js"></script> <link href= ...

  8. make编写教程(二)

    1. make中的变量 makefile中的变量就是c/c++中的宏 2. 引用其他的make文件 类似于c语言中的#include,被包含的文件会原模原样的放在当前文件的包含位置. include& ...

  9. 文本处理工具——sed基础

    一sed介绍 三剑客是grep,sed,awk,功能都很强大. 其中sed是Stream EDitor,流编辑器 行,编辑器的简写,它一次处理一行内容. sed的强大在于可以对文件进行修改,很适合在脚 ...

  10. 【Flutter学习】页面布局之基础布局组件

    一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...