当前位置:网站首页>【力扣】35. 搜索插入位置
【力扣】35. 搜索插入位置
2022-06-28 15:58:00 【e1373773】
PHP代码:
不知道为什么,力扣上面无法提交PHP的题解,是只有我这样么。。。
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function searchInsert($nums, $target) {
$left = 0;
$right = count($nums) - 1;
if ($target > $nums[$right]) return count($nums);
if ($target < $nums[0]) return $left;
while ($left <= $right) {
$mid = intval($left + ($right - $left) / 2);
if ($target < $nums[$mid]) {
$right = ($mid) - 1;
} else if ($target > $nums[$mid]) {
$left = ($mid) + 1;
} else {
return $mid;
}
}
return $left;
}
}解题思路(二分查找):
intval($left + ($right - $left) / 2);PHP的整除运算结果可能不会是整数,这个需要注意。
其他的可以参考代码本身。
边栏推荐
- PostgreSQL异常处理
- Tiktok actual battle ~ list of bloggers I follow, follow and check
- 关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger
- Vc2010 compilation qt5.6.3 prompt cvtres: fatal error cvt1107:
- 抖音实战~我关注的博主列表、关注、取关
- 【Proteus仿真】L297驱动步进电机
- QT interface library
- 5分钟的时间制作一个反弹球游戏
猜你喜欢

5 minutes to make a bouncing ball game

【MySQL】官网文档学习之查询语句sql注意事项

北京有哪些牛逼的中小型公司?

大神详解开源 BUFF 增益攻略丨直播讲座

Big God explains open source buff gain strategy live lecture

薅羊毛的机会了,点个“赚”即有机会赚取高额佣金
![[MySQL] official website document learning query statement SQL precautions](/img/aa/bf27b609e2fda1edaa46f134fc5626.png)
[MySQL] official website document learning query statement SQL precautions

IPDK — Overview

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse

【Hot100】4. 寻找两个正序数组的中位数
随机推荐
A little hesitant in the morning
Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
leetcode:22. bracket-generating
How can the sports app keep the end-to-side background alive to make the sports record more complete?
NFT pledge LP liquidity mining system development details
开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
MySQL auto - Connect Query recommended favorites
LDD 知识整理
IPDK — Overview
昨日元宇宙|Meta “元宇宙”部门一季度亏损29.6亿美元,六福珠宝发行数字藏品
10:00面试,10:02就出来了 ,问的实在是太...
RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52
Super automation and the future of network security
IPDK — Overview
【高并发基础】MySQL 不同事务隔离级别下的并发隐患及解决方案
Android和eclipse和MySQL上传图片并获取
机器学习之卷积神经网络使用cifar10数据集和alexnet网络模型训练分类模型,安装labelimg,以及报错ERROR