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. 将一个对象赋值给另一个对象(使用element CheckBox中length报错)

    注意两个对象相似(比如form表单),千万不要直接赋值(会把对象的属性也变化),很容易漏掉一些属性.比如此次CheckBox报length的错误,就是因为用于存放checkbox复选框选项的数组进过赋 ...

  2. C# Aspose.Words 数据写入到word,模板样式复杂(转换指定内容并返回多张图片)

    public ResultResponse<string[]> PrintStudyRecords([FromBody]StudyInfo info) { ResultResponse&l ...

  3. mysql基于GTIDS复制

    GTIDS的环境:一台master 192.168.200.111多个slave: 192.168.200.112 192.168.200.113 修改master服务器:[root@localhos ...

  4. log4j.xml配置详解(转)

    转自:http://willow-na.iteye.com/blog/347340 Xml代码 <?xml version="1.0" encoding="UTF- ...

  5. Django Rest框架 APIView源码调用

    上一篇说了请求访问的流程,这一篇说一下请求对应的源码调用 as_view 定义view dispatch dispatch initialize_request get_parsers         ...

  6. Oracle修改数据文件路径

    更改Oracle数据文件名及数据文件存放路径 SQL> select * from v$dbfile;      FILE# NAME---------- ------------------- ...

  7. 最大流的SAP算法模板

    明天补充~~~先上代码 #include<iostream> #include<string> #include<queue> #include<vector ...

  8. spring mvc 整合 druid

    环境: ubuntu eclipse maven 一. pom.xml 加入druid 依赖 <!-- https://mvnrepository.com/artifact/com.alibab ...

  9. 【JVM】内存区域

    程序运行时,有六个地方都可以保存数据: 1. 寄存器:这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部.然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配.我们对此没有 ...

  10. vue中引入路由,如果你懒得写那么

    可以npm i vue-router --save,项目中自动给你写好,vuex也可以