26. Remove Duplicates from Sorted Array
来源:互联网 发布:java基础讲解实战 编辑:程序博客网 时间:2024/05/16 19:25
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.
我采用的方法如下:
def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ k = len(nums)#get the length of nums if (k == 0): return 0 elif (k == 1): return 1 else: i = 0 while i < k: j = i + 1 if (j == k): break if (nums[i] == nums[j]): del nums[j] k = len(nums) else: i = j return k其他的方法:
class Solution(object): def removeDuplicates(self, nums): if len(nums)==0: return 0 j=0 for i in range(1,len(nums)): if nums[i]!=nums[j]: nums[j+1]=nums[i] j=j+1 return j+1
阅读全文
0 0
- [LeetCode]26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode --- 26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- [Leetcode] 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 【leetcode】26. Remove Duplicates from Sorted Array
- 【leetcode】 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- [leetCode]26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array LeetCode
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 10.24 环境变量PATH,cp,mv,文档查看cat/more/less/head/tail
- SPI协议及工作原理
- opencv数字识别
- 学生信息录入
- C语言
- 26. Remove Duplicates from Sorted Array
- Statement resuliset
- 验证手机及邮箱的封装函数
- 按钮布局方式演示
- PCB- 蛇形走线的作用
- JavaScript 对象
- 机器学习_决策树
- pandas中关于时间的处理总结
- Java String中常用方法