当前位置:网站首页>LeetCode_35(搜索插入位置)
LeetCode_35(搜索插入位置)
2022-07-01 04:37:00 【***】
题目描述:
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 O(log n) 的算法。
示例 1:
输入: nums = [1,3,5,6], target = 5
输出: 2
示例 2:
输入: nums = [1,3,5,6], target = 2
输出: 1
示例 3:
输入: nums = [1,3,5,6], target = 7
输出: 4
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104 nums 为 无重复元素 的 升序 排列数组
-104 <= target <= 104
class Solution {
public int searchInsert(int[] nums, int target) {
int res=-1;
int left=0,right=nums.length-1,pos=0;
while(left<=right){
pos=(left+right)/2;
if(target>nums[pos])left=pos+1;
else if(target<nums[pos])right=pos-1;
else if(target==nums[pos])return pos;
if(left>right)return left;
}
return res;
}
}
边栏推荐
- Web server: how to choose a good web server these five aspects should be paid attention to
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- 2022年聚合工艺考试题及模拟考试
- Daily question - line 10
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- 2022年上海市安全员C证考试题模拟考试题库及答案
- Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
- Programs and processes, process management, foreground and background processes
猜你喜欢

Difficulties in the development of knowledge map & the importance of building industry knowledge map

Simple implementation of slf4j

【LeetCode】100. Same tree

CF1638E. Colorful operations Kodori tree + differential tree array
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

Shell之一键自动部署Redis任意版本

2022危险化学品生产单位安全生产管理人员题库及答案

2022 gas examination question bank and online simulation examination

Question bank and answers for chemical automation control instrument operation certificate examination in 2022
![[recommended algorithm] C interview question of a small factory](/img/ae/9c83efe86c03763710ba5e4a2eea33.jpg)
[recommended algorithm] C interview question of a small factory
随机推荐
神经网络-非线性激活
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
Pytest automated testing - compare robotframework framework
2022-02-15 (399. Division evaluation)
Rule method: number of effective triangles
AssertionError assert I.ndim == 4 and I.shape[1] == 3
About the transmission pipeline of stage in spark
Odeint and GPU
Leecode records the number of good segmentation of 1525 strings
2022年煤气考试题库及在线模拟考试
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
Pytorch(四) —— 可视化工具 Visdom
Openresty rewrites the location of 302
Advanced application of ES6 modular and asynchronous programming
One click shell to automatically deploy any version of redis
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
离线安装wireshark2.6.10
Shell之一键自动部署Redis任意版本
2022危险化学品生产单位安全生产管理人员题库及答案
细数软件研发效能的七宗罪