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

丢失的数 | 简单级-算法 #13

Open
OBKoro1 opened this issue Aug 1, 2019 · 0 comments
Open

丢失的数 | 简单级-算法 #13

OBKoro1 opened this issue Aug 1, 2019 · 0 comments

Comments

@OBKoro1
Copy link
Owner

OBKoro1 commented Aug 1, 2019

博客链接

# 丢失的数

# 难度:简单

# 描述:

在数组 A 中,包含 0 到 n 的整数,其中缺失了一个数。请编写代码,以查找数组中缺失的整数。

# 样例:

array = [4,3,2,0,5] return 1

array = [0,1,2,3,4,7,6] return 5

array = [0,1,2,3] return 4

# 思路分析:

简单,不分析

# 代码模板:

const findMissing = arr => {};

# 想一想再看答案

# 想一想再看答案

# 想一想再看答案

# 代码:

遍历数组长度的n+1次(包括0),对比有没有这个值即可

const findMissing = arr => {
  let num = arr.length + 1;
  for (let i = 0; i < num; i++) {
    if (arr.indexOf(i) === -1) {
      // i不在数组中 就找到这个值
      return i;
    }
  }
};
console.log('输出', findMissing([4, 3, 2, 0, 5])); //1
console.log(findMissing([0, 1, 2, 3, 4, 7, 6])); // 5
console.log(findMissing([0, 1, 2, 3])); // 4

# 点个Star支持我一下~

博客链接

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant