LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑
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 解答及疑惑的更多相关文章
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 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 ...
- LeetCode记录之26——Remove Duplicates from Sorted Array
国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- 【leetcode❤python】26. Remove Duplicates from Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def removeDuplicates(self, nums): "&quo ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 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++> 给出排序好的 ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- [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 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
随机推荐
- 算法8-5:Prim算法
Prim算法用于计算最小生成树.Prim算法分为两种,一种是懒汉式,一种是饿汉式. 懒汉式Prim 懒汉式Prim算法过程例如以下: 首先将顶点0增加到MST中 从MST与未訪问顶点之间边中选出最短的 ...
- Xfce4: 显示/隐藏 休眠/混合睡眠/挂起 按钮
可以在xfconf配置项中创建如下属性控制: xfconf-query -c xfce4-session -np '/shutdown/ShowHibernate' -t 'bool' -s 'fal ...
- go语言从例子开始之Example10.map(字典)
map 是 Go 内置关联数据类型(在一些其他的语言中称为哈希 或者字典 ) package main import "fmt" func main() { 要创建一个空 map, ...
- go语言从例子开始之Example11.range遍历
range 迭代各种各样的数据结构.让我们来看看如何在我们已经学过的数据结构上使用 rang 吧. package main import "fmt" func main() { ...
- 本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
转载:https://blog.csdn.net/sunshinegyan/article/details/80017012 前提:本地已经安装好了MongoDB服务 1启动MongoDB: 方法1: ...
- Python技能树
本博客Python内容的索引,以后就照着它写了.
- double中首字母大写与小写的区别
Double 是类 double是基础数据类型.Double类型是double的包装类.Double 和double之间的相互转化称为自动拆箱和自动装箱.如果从对象角度理解,那么Double就是对象, ...
- 每天一个linux命令:file(11)
file file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程. 格式 file [选项] [参数] 参数选项 参数 备注 -b 列出辨识结果时,不 ...
- 06-图2 Saving James Bond - Easy Version(25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- kubernetes使用kubeadm升级集群
升级前准本 官网: https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-upgrade/查看可升级的组件 [root@h ...