找到2名失踪的数字数组中的最快方法 [英] Fastest way to find 2 missing numbers in an array

查看:166
本文介绍了找到2名失踪的数字数组中的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题存在,只是因为单纯的好奇心。没有一门功课。

This question exist only because of pure curiosity. Not a homework.

找到最快的方式找到两名失踪人数在一个数组1..1

Find the fastest way to find two missing number in an array 1..n

因此​​,在相关的帖子:<一href="http://stackoverflow.com/questions/2113795/quickest-way-to-find-missing-number-in-an-array-of-numbers">Quickest方式找到在数字数组漏泄次数的 我发现,你可以通过总结和减去总很快做到这一点pretty的。

So, In a related post: Quickest way to find missing number in an array of numbers I found out that you can do this pretty quickly by summing up and substracting total.

但对于2号?

所以,我们的选择是:

  1. 顺序搜索
  2. 在总结项目,从总量减去从1..N的所有项目,然后搜索所有可能的情况。

还有别的吗? 可能有O(n)的解决方案? 我的网站之一的红宝石部分中找到这一点,但任何语言被视为(除非有一个语言中的一些具体的东西)

Anything else? Possible to have O(n) solution? I found this in ruby section of one of the websites, but any language is considered (unless there are some specific things for a language)

推荐答案

最简单的方法(和pretty的快呢:)

The simple way (and pretty fast too:)

a = [1,2,3,5,7]
b = (1..7).to_a
p b-a #=> [4, 6]

这篇关于找到2名失踪的数字数组中的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆