【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

时间:2021-10-06 09:39:30

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy

题目描述

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 = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

题目大意

从一个有序数组中删除重复的数字,只保留下无重复的有序数组,把这些数字放到原数组的前面部分,返回这部分的长度。

解题方法

双指针

慢指针指向应该放入元素的位置,每次移动一格。快指针找到应该放哪个元素,每次找到下一个新的元素。

Python代码如下:

class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
N = len(nums)
if N <= 1: return N
left, right = 0, 1
while right < N:
while right < N and nums[right] == nums[left]:
right += 1
left += 1
if right < N:
nums[left] = nums[right]
return left

C++代码如下:

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
const int L = nums.size();
if (L <= 1) return L;
int slow = 1;
int fast = 1;
while (fast < L) {
while (fast < L && nums[fast] == nums[fast - 1]) {
fast ++;
}
if (fast < L) {
nums[slow] = nums[fast];
slow ++;
fast ++;
}
}
return slow;
}
};

Java代码如下:

public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}

日期

2016 年 05月 8日
2019 年 9 月 17 日 —— 听了hulu宣讲会,觉得hulu的压力不大

【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array &lbrack;Array&sol;std&colon;&colon;distance&sol;std&colon;&colon;unique&rsqb; &lt&semi;c&plus;&plus;&gt&semi;

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  2. leetCode 26&period;Remove Duplicates from Sorted Array&lpar;删除数组反复点&rpar; 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  3. &lbrack;LeetCode&rsqb; 26&period; Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  4. &lbrack;LeetCode&rsqb; 26&period; Remove Duplicates from Sorted Array &star;&lpar;从有序数组中删除重复项&rpar;

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  5. Java &lbrack;leetcode 26&rsqb;Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  6. LeetCode 26&period; Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. LeetCode 26 Remove Duplicates from Sorted Array

    Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  8. Leetcode 26&period; Remove Duplicates from Sorted Array &lpar;easy&rpar;

    Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...

  9. &lbrack;leetcode&rsqb;26&period; Remove Duplicates from Sorted Array有序数组去重&lpar;单个元素只出现一次&rpar;

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

随机推荐

  1. android性能测试与调优:使用 DDMS 查看内存分配情况

    1. 启用自己的APK后 2. 点击左边更新heap 3. 点击右边的heap中的垃圾回收cause GC,等待数秒出现回收内存与数据情况(由于内存回收了APK运行出现异常crash) 4. 点击一个 ...

  2. HDU ACM 1051&sol; POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 去掉ILDasm的SuppressIldasmAttribute限制

    原文:去掉ILDasm的SuppressIldasmAttribute限制 今天本打算汉化一个.Net程序的,当用ILDasm打开的时候,出现了"受保护模块—无法进行反汇编"的错误 ...

  4. mysql 与linux ~ 内存分析与调优

    一 简介:linux内存和mysql二 分类   1 用户空间和内核空间      用户空间内存,从低到高分别是五种不同的内存段      1 只读段 包含代码和常量等      2 数据段 包含全局 ...

  5. MySQL全文本搜索

    启用全文本搜索支持 create table text( -> id int not null auto_increment, -> texts text null, -> prim ...

  6. ios开发怎样才能做到代码和界面彻底分离,方便换肤?

    设想一下,你现在手底下有N个开发人员,你如何让这些人参与到一个ios开发项目中来?而不是独自一个人完成.

  7. 文件系统、服务、防火墙、SELINUX——安全四大金刚

    一提到安全,大家都会想到防火墙,和文件系统权限.而实际工作环境中,我们在Linux的安全配置,会涉及到四个级别.我们思考一个场景,你要在百度盘中存放一个文件,这个动作需要考虑下面四个权限. 1 fir ...

  8. QuantLib 金融计算——随机过程之概述

    目录 QuantLib 金融计算--随机过程之概述 框架 用法与接口 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--随机过程之概述 载入模块 import Q ...

  9. 如何配置FastReport&period;Net环境

    利用FastReport.Net工具箱中的EnvironmentSettings组件可以控制FastReport.Net的环境设置.把EnvironmentSettings组件放到窗体上,并使用下面的 ...

  10. beego

    https://www.kancloud.cn/hello123/beego/126087