当前位置:网站首页>leetcode009--用二分查找在数组中搜索目标值
leetcode009--用二分查找在数组中搜索目标值
2022-04-23 04:38:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class test09 {
/**
*
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为 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
示例 4:
输入: nums = [1,3,5,6], target = 0
输出: 0
示例 5:
输入: nums = [1], target = 0
输出: 0
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums 为无重复元素的升序排列数组
-104 <= target <= 104
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] nums0 = br.readLine().split(",");
String target0 = br.readLine();
int target = Integer.parseInt(target0);
int[] nums = new int[nums0.length];
int i = 0;
for(String num: nums0){
nums[i++] = Integer.parseInt(num);
}
System.out.println(searchInsert(nums,target));
}
public static int searchInsert(int[] nums, int target){
int n = nums.length;
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) / 2) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124357092
边栏推荐
- test
- Brushless motor drive scheme based on Infineon MCU GTM module
- 383. 赎金信
- C language character constant
- What is the thirty-six plan
- io.Platform.packageRoot; // ignore: deprecated_member_use
- Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
- Common string processing functions in C language
- Mysql---数据读写分离、多实例
- 【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
猜你喜欢
383. Ransom letter
520. Detect capital letters
补:注解(Annotation)
Express middleware ① (use of Middleware)
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
Coinbase:关于跨链桥的基础知识、事实和统计数据
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
Introduction to Cortex-M3 register set, assembly language and C language interface
Coinbase: basic knowledge, facts and statistics about cross chain bridge
随机推荐
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Redis command Encyclopedia
eksctl 部署AWS EKS
单片机串口数据处理(1)——串口中断发送数据
What is the thirty-six plan
[mapping program design] coordinate azimuth calculation artifact (version C)
Why recommend you to study embedded
[paper reading] [3D object detection] voxel transformer for 3D object detection
在AWS控制台创建VPC(无图版)
记录一下盲注脚本
Use recyclerview to realize left-right side-by-side classification selection
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
Supplement: Annotation
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
无线键盘全国产化电子元件推荐方案
zynq平臺交叉編譯器的安裝
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation