leetcode26. [Array]Remove Duplicates from Sorted Array
2016-03-07 20:20
531 查看
Total Accepted: 117053 Total Submissions: 354699 Difficulty: Easy
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.
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.
class Solution(object): def removeDuplicates(self, nums): j=1;count=0 if not len(nums): return 0 tmp=nums[0] for i in range(1,len(nums)): if nums[i]!=tmp: nums[j]=nums[i] j=j+1 tmp=nums[i] else: count=count+1 for i in range(count): nums.pop() return len(nums)
相关文章推荐
- 《机电传动控制》第二周作业
- ListView的性能优化之convertView和viewHolder
- Ecshop购物车或结算页面显示商品的品牌
- C++模板插入排序
- 00.出关啦!!!
- 一个java代码生成器的简单实现
- NLP 课程笔记
- 基于ZooKeeper的Dubbo注册中心
- ASP.NET MVC 4 中 Controller 与 ApiController 做读取、新增、更新、删除 ( CRUD )
- UI界面的一些简单控件
- 1282 回文数猜想
- mac系统中让一台电脑多套键盘鼠标同时多人使用
- pjsip教程-ICE-NAT passthrough
- Struts2入门示例教程
- 同余模定理
- java设计模式之单例模式
- 在淘宝上买件东西,背后发生了什么?(简述)
- HDU-3613 Best Reward (Manacher)
- ASP.NET Web API是什么?
- C#事件