[算法题] Remove Duplicates from Sorted Array
题目内容
本题来源于LeetCode
Given a sorted array, 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 in place with constant memory.
For example,
Given input array 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 new length.
题目思路
本题难度等级: easy
本题要求的是原地进行处理,而且不能够申请新的空间。本题采用的方法是数组当中非常常见的处理方法:快慢指针。
方法是:分别设立两个指针index,i。初始情况下,index和i都指向数组的第一个元素。i为快指针,index为慢指针。 i从第一个元素一直扫描到最后一个元素。index是慢指针,仅当nums[i]!=nums[index]的时候,index才会自增1。当扫描结束的时候,index指向的是不重复数组的最后一个元素,其长度为index+1
Python代码
class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if not A:
return 0
index= 0
for i in range( len(A)):
if A[i] != A[index]:
index+= 1
A[index] = A[i]
return index+ 1
[算法题] Remove Duplicates from Sorted Array的更多相关文章
- [算法题] Remove Duplicates from Sorted Array ii
题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...
- 【算法】LeetCode算法题-Remove Duplicates from Sorted Array
这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...
- 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算法题-Remove Duplicates from Sorted List
这是悦乐书的第160次更新,第162篇原创 01 前情回顾 昨晚的爬楼梯算法题,有位朋友提了个思路,使用动态规划算法.介于篇幅问题,这里不细说动态规划算法,以后会在数据机构和算法的理论知识里细说. 昨 ...
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 算法题丨Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
随机推荐
- JavaScript从入门到忘记
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 二.变 ...
- 懒人的小技巧, 批处理修改IP
相信很多人都有这样的麻烦, 工作单位的IP网段与住的不一致, 自己的笔记本在单位和回家的时候每次都要更改IP, 很麻烦, 偷个懒, 做了个批处理来修改IP,方便一点. 还有就是可以把工作的时候才需要 ...
- AT&T汇编helloworld
摘自:http://blog.163.com/guixl_001/blog/static/417641042012112102642703/ 代码: #hello.s .data # 数据段声明 ms ...
- NEWS-包名-baseTest-类名-baeseDao
package baseTest; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedS ...
- java环境配置,试用和基本数据结构
一.java环境配置 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值:jdk文件所在的路经变量名:CLASS ...
- HTML应用程序(HTML App)
HTML应用程序(HTML App) 一个简单的 html app例子: <HTML><HEAD><TITLE>hta示例</TITLE><HTA ...
- Java之初识
今天开始学习Java 1.什么是Java? Java是1995年由sun公司推出的一门极富创造力的面向对象编程语言,是由Java之父詹姆斯格斯林博士设计的. Java名字的由来:据说,java刚刚设计 ...
- 是否使用安全模式启动word
打开word,出现了一个提示,显示着“word遇到问题需要关闭.我们对此引起的不便表示抱歉.”下面有选项“恢复我的工作并重启word”,选中它.点下面的“不发送”. 在出现的提示 ...
- SQL Server 2008R2的安装
一.安装前的准备工作:SQL Server 200R2安装包 二.SQL Server2008R2的安装 1.打开SQL Server2008R2的安装包,找到setup.exe 2.双击sql se ...
- 关于"模块计算机类型与目标计算机类型冲突"的解决
问题描述:我的64位工程包含32位静态库之后报错(模块计算机类型"x86"与目标计算机类型"x64"冲突),将工程修改为32位之后,又报错(若干个无法解析的外部 ...