[LeetCode]题解(python):026-Remove Duplicates from Sorted Array
题目来源:
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的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- LeetCode 026 Remove Duplicates from Sorted Array
题目描述:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such t ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 【LeetCode】026. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 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 ...
- 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
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- LeetCode(28)-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 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等. ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
随机推荐
- 脑波设备mindwave二次开发框架
神念科技提供的mindwave提供了脑波耳机和相应的游戏,这些游戏你可以通过购买神念科技的mindwave耳机来获取,这里不多作介绍. 我们作为程序员,如果有了相应的创意,也可以通过他们提供的二次开发 ...
- HDU 5729 Rigid Frameworks(连通性DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5729 [题目大意] 给出一个n*m的方格框,可以在单位矩形中添加两种对角线的线,使得其变得稳定,问 ...
- perl笔记
更精确的空白字符串匹配 1.水平空白字符 Perl5.10之前,使用\s这种分类,不够精确,容易导致意料外的结果: Perl5.10中引入\h字符组,用来匹配任意水平空白字符(包括Unicode字符集 ...
- PASCAL的读入优化
没readkey的情况 type Tstring=record s:array[0..maxn] of char; n:longint; end; procedure scan(var S:Tstri ...
- objective-C Ⅱ
objective-C Ⅱ 接第一讲 objective-c初识 一.oc中的数组:NSArray 定义: NSArray *arrayName=[NSArray arrayWithObje ...
- sql 分割字符串 存储过程
默认是用 , 分割,如有需要可以自己更改 我写的是 循环插入数据 存储过程,也可以自己改成 方法使用 存储过程: ),@jrid int) as begin declare @location int ...
- If the server requires more time, try increasing the timeout in the server editor
双击服务器,在overview下的Timeouts中的Start选项,改成10000或者较大就可以了.防止服务器自启动频繁.
- ELK(ElasticSearch, Logstash, Log4j)系统日志搭建
1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...
- java 多个文件打包zip
/** * 多个文件打包成zip */ public class ZipDemo { private static void create() throws Exception{ String pat ...
- codeforces 616E. Sum of Remainders 数学
题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...