当前位置:网站首页>【力扣】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的整除运算结果可能不会是整数,这个需要注意。
其他的可以参考代码本身。
边栏推荐
- Code implementation of gain (4) -- gap dataset missing data filling based on GaN (sequence) [improved version]
- Traffic management and control of firewall Foundation
- 24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我
- 平台即代码的未来是Kubernetes扩展
- Visual studio 2019 software installation package and installation tutorial
- Etcd可视化工具:Kstone简介(一)
- 薅羊毛的机会了,点个“赚”即有机会赚取高额佣金
- 岛屿类问题通用解法与DFS框架
- 关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
- 请问下大家有遇到过这种设置的主健和数据库一致的错误吗?
猜你喜欢

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

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

IPDK — Overview
![[MySQL] official website document learning query statement SQL precautions](/img/aa/bf27b609e2fda1edaa46f134fc5626.png)
[MySQL] official website document learning query statement SQL precautions

Traffic management and control of firewall Foundation

No win32/com in vs2013 help document

OpenHarmony—内核对象事件之源码详解

Azure Kinect微软摄像头Unity开发小结

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

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
随机推荐
A 24-year-old bald programmer teaches you how to continuously integrate and deliver microservice delivery. You can't learn how to cut me off
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels
Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
ID卡复制教程(使用T5577卡复制4100卡)
LDD 知识整理
今天睡眠质量记录80分
Etcd visualization tool: an introduction to kstone (I)
运维-- 统一网关非常必要
【Hot100】4. 寻找两个正序数组的中位数
Convolutional neural networks for machine learning -- an introduction to CNN
访中国信通院王蕴韬:数实融合赋能文化产业繁荣发展
Among US private server setup
软件测试员的悲哀竟是...自己的技术能力不能满足大厂要求?
Android, eclipse and MySQL upload pictures and get
抖音实战~我关注的博主列表、关注、取关
逆向调试入门-PE结构详解02/07
QT interface library
Openharmony - detailed source code of Kernel Object Events
10: 00 interview, came out at 10:02, the question is really too
知道这几个命令让你掌握Shell自带工具