[leetcode] Remove Duplicates from Sorted Array python
来源:互联网 发布:大连淘宝网店装修 编辑:程序博客网 时间:2024/06/05 08:31
problem:
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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): if len(A) == 0: return 0 n = len(A) index = 0 for i in xrange(n): if A[index] != A[i]: index += 1 A[index] = A[i] A = A[:index+1] return len(A)
解法二;
利用python数据结构set的去重功能,但是之后要对A排序。
class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): A = list(set(A)) A.sort() return len(A) #注意题目要求 A 并不是乱序的 set(A)会导致在形成list后是乱序的
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
- Linux关机命令
- 正则表达式在网页处理中的应用四则
- java 命名规范
- 按单词(word)反转字符串(string)输出(output)——不用额外缓存(without a buffer)
- 字符串前缀,真前缀,后缀,真后缀,及前缀函数
- [leetcode] Remove Duplicates from Sorted Array python
- 过敏性鼻炎和感冒的自我快速判断
- oepncms9 汉化
- The connection to adb is down, and a severe error has occured
- ubuntu下gcc和g++的安装
- 双网卡同时访问内外网
- Ubuntu 开机出现 grub rescue> 终端模式修复方法
- 交叉编译Node.js到OpenWrt(HG255D)
- ibatis无法生成主键的原因