【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛
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)的更多相关文章
- 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++> 给出排序好的 ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [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 ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode 26 Remove Duplicates from Sorted Array
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...
- [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 ...
随机推荐
- EXCEL-如何在excel中对图片进行批量排版
新建EXCEL->导入图片->如果每张图高度为33个单元格,共计10张图,那么将最后边的那张图(即正对着你的那一张)剪切粘贴到33*9行第一个单元格处->按F5定位"对象& ...
- 字符串String的trim()方法
用来删除字符串两端的空白字符并返回,trim方法并不影响原来的字符串本身,它返回的是一个新的字符串 String a = " Hello World "; String b = ...
- Vector总结及部分底层源码分析
Vector总结及部分底层源码分析 1. Vector继承的抽象类和实现的接口 Vector类实现的接口 List接口:里面定义了List集合的基本接口,Vector进行了实现 RandomAcces ...
- javaWeb - 1 — servlet — 更新完毕
1.先来聊一些javaWeb相关的知识 简单了解一下:web的发展史 1).web就是网页的意思嘛 2).web的分类 (1).静态web 使用HTML.CSS技术,主要包括图片和文本 优点:简单,只 ...
- 一劳永逸,使用 PicGo + GitHub 搭建个人图床工具
原文链接: 一劳永逸,使用 PicGo + GitHub 搭建个人图床工具 经常写博客的同学都知道,有一个稳定又好用的图床是多么重要.我之前用过七牛云 + Mpic 和微博图床,但总感觉配置起来比较麻 ...
- 学习java的第二十六天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- SQLite is 35% Faster Than The Filesystem
比方说你要在C++/PHP里实现一个函数Image get_image(string id),不同的图片有1万张(用户头像),你可以把它们存在一个目录/文件夹里,然后fopen()再fread. 你也 ...
- LINUX 安装增强 前置安装文件
yum install kernel yum install kernel-devel yum install gcc yum install make
- Linux系统信息查看命令(ZZ)
http://hi.baidu.com/thinkdifferent/blog/item/22f4a80161630e011d958384.html转自一个baidu师兄的博客,很好的一个总结,推荐下 ...
- error信息
/opt/hadoop/src/contrib/eclipse-plugin/build.xml:61: warning: 'includeantruntime' was not set, defau ...