We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
在数组 A 中,包含 0 到 n 的整数,其中缺失了一个数。请编写代码,以查找数组中缺失的整数。
array = [4,3,2,0,5] return 1
[4,3,2,0,5]
array = [0,1,2,3,4,7,6] return 5
[0,1,2,3,4,7,6]
array = [0,1,2,3] return 4
[0,1,2,3]
简单,不分析
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
博客链接
The text was updated successfully, but these errors were encountered:
No branches or pull requests
博客链接
# 丢失的数
# 难度:简单
# 描述:
在数组 A 中,包含 0 到 n 的整数,其中缺失了一个数。请编写代码,以查找数组中缺失的整数。
# 样例:
array =
[4,3,2,0,5]
return 1array =
[0,1,2,3,4,7,6]
return 5array =
[0,1,2,3]
return 4# 思路分析:
简单,不分析
# 代码模板:
# 想一想再看答案
# 想一想再看答案
# 想一想再看答案
# 代码:
遍历数组长度的n+1次(包括0),对比有没有这个值即可
# 点个Star支持我一下~
博客链接
The text was updated successfully, but these errors were encountered: