题目来源:

  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. bootstrap 导航栏

    非常好的一篇文章: http://webdesigntutsplus.s3.amazonaws.com/tuts/312_bs/My-Bootstrap-Site-NAVBAR/navbar-exam ...

  2. HDU 2167 Pebbles

    题目大意:有个N*N( 3<=N<=15 )方阵, 可从中若干个数, 使其总和最大.取数要求, 当某一个数被选, 其周围8个数都不能选. 题解:记s数组为合法状态,即没有相邻的数字同时被选 ...

  3. css 3种清除浮动方法

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  4. 如何在内存中压缩并加密ZIP

    项目中遇到了一个问题,考虑到安全原因,需要将文件以二进制数据的方式打包成压缩文件,并且这个压缩文件是有密码的. 去Google上找了些API,下载来看了下,琢磨出了以下方法 首先放API: <! ...

  5. git切换远程

    已经开发一段时日,公司突然提出要换git仓库 查看目前所有的分支 $git branch -va 添加新的远程仓库 $ git remot add [name] [url] 查看下目前配置 $ git ...

  6. 加密PHP文件的方式,目测这样可以写个DLL来加密了

    <?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1 ...

  7. [LeetCode]题解(python):122-Best Time to Buy and Sell Stock II

    题目来源: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意分析: 和上题类似,给定array,代表第i天物品i ...

  8. android天气查询(一)websevice之ksoap2软件包的使用

    对于用到天气信息,首先我想: 第一:数据不可能是我测得的,必须是网上的信息. 第二:网上的信息分为好多种,具体哪种比较好一点,这里我总结了两种. 第三:数据JSON怎么解析. 第四:如何提出数据与显示 ...

  9. libcurl的使用问题“Expect100-continue”

    最近在做团购酒店APP分享到qzone功能,使用libcurl访问qzone的分享cgi接口,酒店分享信息以POST方式传输,在测试的时候发现分享接口平均有2s的延迟,这延迟也太大了吧,于是乎问了空间 ...

  10. HDU 2152 Fruit

    系数为1的母函数…… #include <cstdio> #include <cstring> using namespace std; int n,m,size[105][2 ...