【Leetcode】Set Mismatch

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

1 第一种方法,用m = collection.Counter[nums]的对nums计数,再遍历nums中的数,找出m[num]==2的num即是第一个数

2 得出原始list的sum,再得出error list的sum,通过和first数结合算出second数。这种方法时间复杂度只超过了23%

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,178评论 0 10
  • 它就像一位纯洁天真的害羞少女,虽然她不是那种立刻会吸引住人们目光的女子,但是,越仔细看就越能发现她的美丽和优点。纯...
    阿温_ecdd阅读 3,514评论 0 0
  • 有时候常常会想,我们是不是对自己太过严苛了?我们熬夜,早起,要做这个要做那个。 看到好的书法作品,自己也想要写出...
    松鼠会唱歌阅读 7,357评论 2 3
  • 我常常羡慕一种人,不论发生事情都不会放在心上,该吃吃,该玩玩,该睡睡。似乎天塌下来也不会影响他的心情。俗称心大。 ...
    北佳人阅读 4,406评论 1 2
  • 在刚刚结束的超级750丹麦羽毛球公开赛上,中国台北选手“影帝”周天成1∶2不敌日本选手中桃田贤斗,获得亚军...
    俗而不雅阅读 1,796评论 0 1

友情链接更多精彩内容