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

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.

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

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

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)