Set Mismatch

    这道题是简单题

  题目:

    

  思路:

    1、我的思路:for循环1到n的自然数,nums列表删除i,如果捕获到异常,就在后面nums后面添加i,最后返回nums。但是这样做复杂度太高,程序运行会超时,所以需要改进

    2、另外加入一个列表b,循环nums列表对应b[i] + 1,最后遍历b列表,返回b[i]等于2和0的i值

  代码:

    1、超时代码:

 1 class Solution(object):
 2     def findErrorNums(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[int]
 6         """
 7
 8         for i in range(1,len(nums)+1):
 9             try:
10                 nums.remove(i)
11
12             except ValueError:
13                 nums.append(i)
14         return nums

    2、通过代码:

  

 1 class Solution(object):
 2     def findErrorNums(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: List[int]
 6         """
 7
 8         a = len(nums)
 9         b = [0] * (a+1)
10         for i in nums:
11             b[i] += 1
12         for i in range(1, a+1):
13             if b[i] == 2: c = i
14             if b[i] == 0: d = i
15         return c, d
时间: 2024-08-06 05:24:19

Set Mismatch的相关文章

Jar mismatch! Fix your dependencies的解决方案

最近在unbuntu中编译android项目的时候遇到了很多问题,最近遇到的问题是Jar mismatch! Fix your dependencies好像是缺少依赖,要解决其中的依赖关系. 在开发Android项目的时候,有时需要引用多个项目作为library.在引用项目的时候,有时会出现"Jar mismatch! Fix your dependencies"错误.这是因为两个项目的jar包(android-support-v4.jar)不一致. 解决方法是把2个jar都删除,然后

HibernateSystemException: ordinal parameter mismatch

使用Hibernate + MySQL数据库,写HQL语句时,不要忘了不支持MySQL的limit 来自为知笔记(Wiz)HibernateSystemException: ordinal parameter mismatch,码迷,mamicode.com

apt-get update 更新 ubuntu时出现Hash sum mismatch的原因及解决方法

$ sudo apt-get update ...... Hit http://mirrors.163.com trusty/main Sources                                 Hit http://mirrors.163.com trusty/restricted Sources                           Get:17 http://mirrors.163.com trusty/universe Sources [6,399 kB

Oracle启动时提示map size mismatch; abort

今天在DELL服务器的Redhat 6.4上安装Oracle10.2.0.4,用sqlplus 登陆数据库,会出现如下的提示: map size mismatch; abort: Success,不过其他的功能都正常,只是有这个提示 解决方法: alter system set "_enable_NUMA_optimization"= false scope=spfile; alter system set "_db_block_numa"=1 scope=spfi

Jar mismatch! Fix your dependencies

1.在Android开发中,会遇到包名不一致的情况,如下所示 [2015-11-21 11:56:03 - test] Found 2 versions of android-support-v4.jar in the dependency list, [2015-11-21 11:56:03 - test] but not all the versions are identical (check is based on SHA-1 only at this time). [2015-11-2

libtool: Version mismatch error 解决

在编译一个软件的时候,在 ./configure 和 make  之后可能会出现如下错误: [plain] view plaincopyprint? libtool: Version mismatch error.  This is libtool 2.4.2 Debian-2.4.2-1ubuntu1, but the libtool: definition of this LT_INIT comes from libtool 2.4. libtool: You should recreate

svn: E155017: Checksum mismatch while updating 校验错误的解决方法

[10.3.53.53:/EMRCV5]# svn up svn: E155004: Working copy '/EMRCV5' locked. svn: E155004: '/EMRCV5' is already locked. svn: run 'svn cleanup' to remove locks (type 'svn help cleanup' for details) [10.3.53.53:/EMRCV5]# svn cleanup [10.3.53.53:/EMRCV5]#

file download hash mismatch

在linux中使用cmake时,遇到了"file download hash mismatch",同时status显示"unsupported protocol". google发现问题出在cmake,如同一位网友所言:Based on the error message "unsupported protocol", you probably build CMake without OpenSSL support. 那么解决方案就是build

java.lang.Error: Unresolved compilation problems: Syntax error on token "return", delete this token Type mismatch: cannot convert from Init to String

java.lang.Error: Unresolved compilation problems:   Syntax error on token "return", delete this token  Type mismatch: cannot convert from Init to String Dubbo 在调用服务时候报的错误! 那么问题来了.这什么错? ? 英语不好.     Type mismatch: cannot convert from Init to Strin

ubuntu 安装时遇到 hash sum mismatch 处理方法

ubuntu安装大软件时,下载经常容易出错,hash sum mismatch是其中一种,说到底还是网络不好,重试很多遍都是这个错误,最后的解决方案是把mismatch说的那个链接用firefox打开并下载,然后 mv 到 /var/cache/apt/archives/ 下面即可 另注:在下载 ros 时,国内的镜像少了东西,报404错误,也用相似的方法解决,去国外的库里下载同路径名的文件,然后放到cahce去就可以了!