26. Remove Duplicates from Sorted Array
2016-07-26 19:36
387 查看
26. Remove Duplicates from Sorted Array
Leetcode link for this questionDiscription:
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.
Analyze:
Code 1 :
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 p=0 for i in nums: if nums[p]!=i: p+=1 nums[p]=i return p+1
Submission Result:
Status: AcceptedRuntime: 96 ms
Ranking: beats 80.80%
Code 2 :
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ le=len(nums) if le<2: return le i=1 while i<len(nums): if nums[i]==nums[i-1]: nums.pop(i) else: i+=1 return len(nums)
Submission Result:
Status: AcceptedRuntime: 144 ms
Ranking: beats 17.31%
相关文章推荐
- DDR Layout Guide-DDR布线规则与过程
- 27. Remove Element
- 顺序表应用1:多余元素删除之移位算法
- 24. Swap Nodes in Pairs
- 2016夏季练习——类次小生成树
- 51Nod-1183-编辑距离
- Python中的序列相机和相乘
- [SCU4438] Censor [2015 Sichuan Province Contest Final C]
- HDU 3363 Ice-sugar Gourd
- Java实现HTML页面转PDF解决方案
- MyBatis和Hibernate相比,优势在哪里?
- 21. Merge Two Sorted Lists
- quartz动态定时任务
- HDU 2141 Can you find it?(二分)
- 20. Valid Parentheses
- DDR Layout Guide-DDR内存布线指导
- hdu_5761_Rower Bo(xjb猜公式)
- 大数小数的加法
- 【Window编程】Step.1 我的第一个Windows程序
- 14. Longest Common Prefix