作者: 负雪明烛
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的压力不大

最新文章

  1. debian/deepin 15.3安装jdk 1.7 (或jdk 7),配置默认环境
  2. php返回数据库查询时出现Resource id #2
  3. 从头来之【图解针对虚拟机iOS开发环境搭建】
  4. C语言程序设计做题笔记之C语言基础知识(上)
  5. 【HDOJ】1811 Rank of Tetris
  6. Gradle[1]gradle distZip时,增加目录信息到zip中
  7. wxpython 拖动界面时进入假死状态(未响应)解决方法
  8. Ubuntu 网管服务器配置
  9. 【Spring系列】自己手写一个 SpringMVC 框架
  10. Flask入门之flask-wtf表单处理
  11. 棋盘游戏,dfs搜索
  12. Java 雇员管理小练习(理解面向对象编程)
  13. 《JavaScript-The Definitive Guide》读书笔记:字符串常用方法
  14. Day10 Python网络编程 Socket编程
  15. Android软键盘遮挡布局问题;
  16. 关于error:Cannot assign to &#39;self&#39; outside of a method in the init family
  17. [CQOI 2018]九连环
  18. Codeforces 750 F:New Year and Finding Roots
  19. solr学习之六--------Analyzer(分析器)、Tokenizer(分词器)
  20. 读TIJ -7 多形性

热门文章

  1. Markdown—.md文件是什么?怎么打开?
  2. Dreamweaver 2019 软件安装教程
  3. C/C++ Qt StringListModel 字符串列表映射组件
  4. 第一个基础框架 — mybatis框架 — 更新完毕
  5. Tomcat类加载机制和JAVA类加载机制的比较
  6. keeper及er表示被动
  7. 大数据学习day35----flume01-------1 agent(关于agent的一些问题),2 event,3 有关agent和event的一些问题,4 transaction(事务控制机制),5 flume安装 6.Flume入门案例
  8. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
  9. LVS nat模型+dr模型
  10. 【C/C++】例题3-5 生成元/算法竞赛入门经典/数组与字符串