题目来源:

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


题意分析:

  给定一个排好序的数组,去除重复的数,返回新数组的长度,不能申请额外的空间,超过新数组长度部分是什么数都无所谓。


题目思路:

  这是一个很简单的题目,由于给定的数组已经排序,那么用i,j两个下标,i记录新数组的下标,j是原来数组下标,如果nums[j] != nums[j - 1],那么nums[i] = nums[j],i 和j 都+ 1。最后返回i。


代码(python):

  

 class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 1
j = 1
size = len(nums)
while j < size:
if nums[j] == nums[i - 1]:
j += 1
else:
nums[i] = nums[j]
i += 1
j += 1
return min(i,size)

转载请注明出处:http://www.cnblogs.com/chruny/p/4885113.html

[LeetCode]题解(python):026-Remove Duplicates from Sorted Array的更多相关文章

  1. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  2. LeetCode 026 Remove Duplicates from Sorted Array

    题目描述:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such t ...

  3. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  4. 【LeetCode】026. Remove Duplicates from Sorted Array

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

  5. Java for LeetCode 026 Remove Duplicates from Sorted Array

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

  6. leetcode第26题--Remove Duplicates from Sorted Array

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

  7. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  8. LeetCode(28)-Remove Duplicates from Sorted Array

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

  9. LeetCode记录之26——Remove Duplicates from Sorted Array

    国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...

  10. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. ViewPager不能高度自适应?height=wrap_content 无效解决办法

    ViewPager用的很多,主要用啦展示广告条.可是高度却不能自适应内容,总是会占满全屏,即使设置android:height="wrap_content"也是没有用的.. 解决办 ...

  2. spring+springMVC集成(annotation方式)

    spring+springMVC集成(annotation方式) SpringMVC+Spring4.0+Hibernate 简单的整合 MyBatis3整合Spring3.SpringMVC3

  3. java MemCachedClient遍历memcache中所有的key

    在java memcached client documentation中没有提共遍历memcache所有key的方法.但是提供了两个方法statsItems和statsCacheDump,通过sta ...

  4. Android多线程任务优化1:探讨AsyncTask的缺陷

     AsyncTask还有别的缺陷,在生成listview的时候,如果adapter里面的count动态改变的话,不能使用AsyncTask,只能使用Thread+Handler,否则会出现如下错误 j ...

  5. Windows Azure 现已完全受 Juju 支持

    我们很高兴地宣布,Windows Azure 现已完全受 Juju 支持,这也是我们为实现开放性和互操作性而不断努力的结果.这意味着 Ubuntu 用户现在可以使用 Juju 及其直观的图形用户界面设 ...

  6. HDU 5059 Help him

    题解:先判断第一个是否负号.如果是把第一个符号拿掉之后判断后面的长度是否<=12,并且是否数字,然后转化成数字看看是否在[a,b],注意-0这个数据. 如果不是判断长度是否<=12,并且是 ...

  7. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  8. django datetime format 日期格式化

    django datetime format 日期格式化 www.jx-lab.com python 中 date,datetime,time对象都支持strftime(format)方法,但有一些区 ...

  9. input输入框只能输入数字的功能

    Html代码 收藏代码 <input type="text" style="ime-mode:disabled;" onpaste="retur ...

  10. Problem C Andy's First Dictionary(set的使用)

    题目链接:Problem C 题意:输入一个文本,找出所有不同的单词,按照字典序从小到大输出,单词不区分大小写. 思路:将字母序列都存为小写,非字母的字符变成空格,然后利用stringstream实现 ...