当前位置:网站首页>【力扣】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的整除运算结果可能不会是整数,这个需要注意。
其他的可以参考代码本身。
边栏推荐
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- Knowing these commands allows you to master shell's own tools
- Qt create 5.0.3 配置Qt4.8.7
- Big God explains open source buff gain strategy live lecture
- Tiktok actual battle ~ list of bloggers I follow, follow and check
- 【初学者必看】vlc实现的rtsp服务器及转储H264文件
- Gartner发布当前至2024年的五大隐私趋势
- among us私服搭建
- 使用openpyxl操作Excel
- FFmpeg之禁止输出banner log(三十)
猜你喜欢

超自动化与网络安全的未来

Internet of things cloud convergence Security Guide

一台服务器最大并发 tcp 连接数多少?65535?

Visual Studio 2019软件安装包和安装教程

平台即代码的未来是Kubernetes扩展

The Web3.0 era is coming. See how Tianyi cloud storage resources invigorate the system to enable new infrastructure (Part 1)

【高并发基础】MySQL 不同事务隔离级别下的并发隐患及解决方案

What are the most powerful small and medium-sized companies in Beijing?

wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module

3. Caller 服务调用 - dapr
随机推荐
抖音实战~我关注的博主列表、关注、取关
访中国信通院王蕴韬:数实融合赋能文化产业繁荣发展
REDIS00_详解redis.conf配置文件
3. caller service call - dapr
QT create 5.0.3 configuring qt4.8.7
Gartner发布当前至2024年的五大隐私趋势
REDIS00_ Explain redis Conf configuration file
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
同创伟业合伙人童子平:“元宇宙”究竟该投什么
Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
VS2013 帮助文档中没有 win32/com
Qt 界面库
机器学习之卷积神经网络Lenet5训练模型
#夏日挑战赛#OHOS构建自定义服务实战
Azure Kinect微软摄像头Unity开发小结
openGauss内核:SQL解析过程分析
【高并发基础】MySQL 不同事务隔离级别下的并发隐患及解决方案
Operating excel with openpyxl