当前位置:网站首页>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;
}
}
边栏推荐
- Talk about testdeploy
- Codeworks round 449 (Div. 1) C. Kodori tree template
- Basic usage, principle and details of session
- Maixll-Dock 使用方法
- How to do the performance pressure test of "Health Code"
- 2022 t elevator repair new version test questions and t elevator repair simulation test question bank
- 神经网络-最大池化的使用
- 2022年上海市安全员C证考试题模拟考试题库及答案
- 为什么香港服务器最适合海外建站使用
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
猜你喜欢

Pytorch(一) —— 基本语法

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

Kodori tree board

Simple implementation of slf4j

Offline installation of Wireshark 2.6.10

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

How to use maixll dock

【LeetCode】100. Same tree

Maixll dock quick start
![Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]](/img/aa/a255d225d71e6ba2b497f8d59f5f11.jpg)
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
随机推荐
Difficulties in the development of knowledge map & the importance of building industry knowledge map
JVM栈和堆简介
This sideline workload is small, 10-15k, free unlimited massage
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
MySQL winter vacation self-study 2022 12 (5)
JS image path conversion Base64 format
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
2022年上海市安全员C证考试题模拟考试题库及答案
Leecode question brushing record 1332 delete palindrome subsequence
C -- array
VIM easy to use tutorial
JS rotation chart
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
CF1638E colorful operations
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
About the transmission pipeline of stage in spark
Dede collection plug-in does not need to write rules