在数字序列中查找第一个缺失的数字
我试图弄清楚如何找到这样的数字序列中的第一个丢失的数字 (1,2,3,5,6,9,10,15)
I am trying to figure out how to find the first missing number of a sequence of numbers like this (1,2,3,5,6,9,10,15)
我想将第一个缺失的数字 #4 放入一个变量中以供以后使用,但不知道该怎么做?
I want to put the first missing number, #4, into an variable for later use but don't know how to do so?
我已经尝试过了,但这只给了我最后一个数字:
I have tried this but this only gives me the last number:
var mynumbers=new Array(1,2,3,6,9,10);
for(var i = 1; i < 32; i++) {
if(mynumbers[i] - mynumbers[i-1] != 1) {
alert("First missing number id: "+mynumbers[i]);
break;
}
}
首先,它会在数字序列中的洞"之后给我第一个数字,其次,如果我不插入中断,它会继续提醒所有在洞"之后出现的数字.我只想要从 1 到 32 的数字序列的第一个缺失数字.我该怎么做?
First of all it gives me the first number after an "hole" in the numbersequence, secondly it continues to alert all numbers comming after an "hole" if I don't insert an break. I only want the first missing number of an numbersequence from 1 - 32. How do i do so?
希望得到帮助并提前感谢 ;-)
Hoping for help and thanks in advance ;-)
推荐答案
这个怎么样
var mynumbers = new Array(1,2,3,6,9,10);
var missing;
for(var i=1;i<=32;i++)
{
if(mynumbers[i-1] != i){
missing = i;
alert(missing);
break;
}
}
相关文章