找出数字序列中第一个缺失的数字

Find the first missing number in a sequence of numbers(找出数字序列中第一个缺失的数字)
本文介绍了找出数字序列中第一个缺失的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

这是我尝试为这个问题寻找解决方案的第二天.

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.

这篇关于找出数字序列中第一个缺失的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why can#39;t I update data in an array with foreach loop?(为什么我不能用 foreach 循环更新数组中的数据?)
Foreach for arrays inside of an array(Foreach 用于数组内的数组)
PHP array get next key/value in foreach()(PHP 数组在 foreach() 中获取下一个键/值)
Using preg_match on a multidimensional array to return key values arrays(在多维数组上使用 preg_match 返回键值数组)
php foreach as key, every two number as a group(php foreach 为key,每两个数字为一组)
Treat a PHP class that implements Iterator as an array(将实现 Iterator 的 PHP 类视为数组)