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 ...
随机推荐
- vue证明题X,vue设置集
1.开发中的控制台tab格式警告隐藏 出现情况如图: 解决方案:找到此代码,注释掉 2.控制台error报告 出现情况如图 解决方案:找到此代码,替换,对浏览器中的警告进行隐藏 遇到就更
- 编译lineageos1
lineageos 前奏 -- 搭建编译环境 我目前使用的手机是红米note4x 目前lineageos15.1已经官方支持,下文是按照官网文档编译安装包操作总结 构建环境搭建主要参考官方文档 参考文 ...
- 运用pool进程池启动大量子进程
# Pool进程池类 from multiprocessing import Pool import os import time import random def run(index): prin ...
- SQL笔试题:下面是学生表(student)的结构说明
SQL笔试题:下面是学生表(student)的结构说明 SQL笔试题:下面是学生表(student)的结构说明 字段名称 字段解释 字段类型 字段长度 约束 s_id 学号 字符 10 PK s_na ...
- 说一说Vuex有哪几种状态和属性
vuex的流程 页面通过mapAction异步提交事件到action.action通过commit把对应参数同步提交到mutation mutation会修改state中对应的值.最后通过getter ...
- anaconda3创建py2环境
查看conda的py环境conda info -e # 创建一个名为python34的环境,指定Python版本是3.4(创建py27操作一样)conda create -n py34 python= ...
- EL属性范围用法sessionScope等(转)
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...
- Mybatis配置——自动使用驼峰命名 属性映射字段(默认为false)
开发一个新项目,用的springboot,相关配置不太熟悉,导致一些配置没配,到具体开发时问题就暴露出来了,记录第一个配置问题----Mybatis配置-自动使用驼峰命名 属性(userId)映射字段 ...
- Django 与 Flask框架的比较
Django Django恐怕是最有代表性的Python框架了.它是一个遵循MMVC架构模式的开源框架.它的名字来自Django Reinhardt,一个法国作曲家和吉他演奏家,很多人认为他是历史上最 ...
- boost string algorithm
The Boost.StringAlgorithms library provides many free-standing functions for string manipulation. 1. ...