这是我尝试为这个问题寻找解决方案的第二天.
It's the second day I try to find a solution for this problem.
我有一个数组.
$datas;
$datas[0]; // 8000
$datas[1]; // 8001
$datas[2]; // 8003
$datas[3]; // 8004
我必须找到从 8000 开始的第一个缺失数字,在这种情况下是 8002.
I have to find the first missing number starting from 8000 in this case it's 8002.
我的想法是做这样的事情:
My idea is to do somethig like this:
$datas[0] +1 = $datas[1]
如果它是真正的端口,它不是免费的,我必须检查下一个,如果它是假的,则它是第一个免费号码.
if it's true port it's not free and I have to check the next one, if it's false it's the first free number.
我知道这不是正确的语法,但我在以正确的方式编写它时遇到了一些问题.
I know it's not a correct syntax but I have some problems writing it in the right way.
从低到高排序(如果还没有排序)然后从最低开始递增,检查是否在数组中:
Sort from lowest to highest (if not already sorted) and then increment from the lowest and check if it is in the array:
sort($datas);
for($i=reset($datas); in_array($i, $datas); $i++);
echo $i;
reset
获取第一个数字,在本例中为 8000 开始,当 $i
不在 $i
中时,in_array
条件终止循环数组.
reset
gets the first number, in this case 8000 to start and the in_array
condition terminates the loop when $i
is not in the array.
这篇关于找出数字序列中第一个缺失的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!