Leetcode-Remove Duplicates from Sorted Array-Python
来源:互联网 发布:cf提示网络出现异常 编辑:程序博客网 时间:2024/06/04 01:23
Remove Duplicates from Sorted Array
给定一个排序数组,将数组中的重复元素去除,并返回修改后的数组长度。
Description
解题思路:
题目要求不能分配额外空间,由于python中列表是传引用,因此可以对传入的列表进行原地修改。由于数组已经排序,因此可以通过新建一个下标两两比较删除重复的元素。
注意:
题目中 It doesn’t matter what you leave beyond the new length,因此超出长度部分的重复元素不需要考虑。
def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 newIndex = 0 for i in range(1, len(nums)): if nums[i] != nums[newIndex]: newIndex += 1 nums[newIndex] = nums[i] return newIndex+1
阅读全文
0 0
- [leetcode] Remove Duplicates from Sorted Array python
- Leetcode-Remove Duplicates from Sorted Array-Python
- 【Leetcode】【python】Remove Duplicates from Sorted Array/Remove Duplicates from Sorted Array II
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- [leetcode] python Remove Duplicates from Sorted Array II
- 【LeetCode with Python】 Remove Duplicates from Sorted Array II
- 26. Remove Duplicates from Sorted Array Leetcode Python
- leetcode 日经贴,python code -Remove Duplicates from Sorted Array
- 【LEETCODE】80-Remove Duplicates from Sorted Array II [Python]
- [leetcode:python]26.Remove Duplicates from Sorted Array
- 【LeetCode 26】Remove Duplicates from Sorted Array(Python)
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array
- Java 基础语法
- 【读书笔记】TCP/IP网络编程 第17章 Linux epoll
- 学习go语言碰到的问题:main redeclared in this block
- 发送短信
- 反射机制
- Leetcode-Remove Duplicates from Sorted Array-Python
- springboot学习笔记-3 整合redis&mongodb【转载】
- Windows10系统不能右键处理文件,一右键就黑屏一下
- HashSet和TreeSet
- MOOC清华《程序设计基础》期末考试第1题:解三元一次方程
- stanford ner训练自己的模型
- Gradle学习系列文章推荐
- 进程控制1之初识进程
- 菜鸡记录之初试自动更新,源码及出现的问题