Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

缺失的数字 #24

Open
Aras-ax opened this issue Mar 27, 2019 · 0 comments
Open

缺失的数字 #24

Aras-ax opened this issue Mar 27, 2019 · 0 comments

Comments

@Aras-ax
Copy link
Owner

Aras-ax commented Mar 27, 2019

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

示例 1:

输入: [3,0,1]
输出: 2

示例 2:

输入: [9,6,4,2,3,5,7,0,1]
输出: 8

说明:

  • 你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?

解答

function missingNumber(num){
    let res = 0,
        l = num.length;
    num.forEach((item, i) => {
        res ^= item ^ (i+1);
    });
    return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant