LeetCode有问题的代码
来源:互联网 发布:淘宝天猫美工 编辑:程序博客网 时间:2024/05/16 15:48
- def findDuplicates(self, nums):
- """
- :type nums: List[int]
- :rtype: List[int]
- """
- #res=[]
- nums.sort()
- for i in nums:
- if nums.count(i)==1:
- nums.remove(i)
- for j in nums:
- nums.remove(j)
- return nums
以上是有问题的代码,解决的是以下的问题:
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:[4,3,2,7,8,2,3,1]Output:[2,3]
时间复杂度很高的方法:
class Solution(object):
def findDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res=[]
nums.sort()
for i in nums:
if nums.count(i)==2:
res.append(i)
#for j in res:
# res.remove(j)
#return res
res=res[::2]
return res
正确的方法:
class Solution(object): def findDuplicates(self, nums): """ :type nums: List[int] :rtype: List[int] """ res=[] for i in range(len(nums)): index=nums[i]-1 if nums[index]<0: res.append(abs(index+1)) nums[index]=-nums[index] return res
搞不清楚我的方法为啥复杂度那么高。
阅读全文
0 0
- LeetCode有问题的代码
- 一段有问题的代码
- 有问题的C语言代码
- 查看项目中有问题的代码
- urlrewrite中多参数的问题 有代码的
- 当提交的代码有问题的时候如何处理??
- 帮我看看我的代码有什么问题
- 这段代码有什么问题吗? 关于IHTMLLinkElement的。
- 《Programming In Lua》书上一个有问题的代码
- 找出php中可能有问题的代码行
- JVM崩溃原因之一:自己的代码有问题
- 发布代码有问题留言
- 有问题的问题?.?
- 有漏洞的代码
- 这段代码哪里有问题?
- android代码签名机制有问题
- 这段代码格式有没有问题?
- 这段Java代码有什么问题???
- java编程思想之操作符
- Laravel操作数据库
- STM32开发环境搭建
- 抽象工厂模式
- 你在那里,我却在这里
- LeetCode有问题的代码
- jquery deferred对象 then(),when()方法 ,解决回调函数嵌套
- oracleの知識ポイント
- 数据结构 — 快速排序
- nginx tar.gz安装方法+简单静态文件配置
- Spring源码阅读(三)—IOC容器依赖注入
- Ubuntu编译Android源码过程中的空间不足解决方法
- 80. Remove Duplicates from Sorted Array II
- 动态规划专项训练(1)