当前位置:网站首页>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
边栏推荐
- 那些年我面试过的Android开发岗总结(附面试题+答案解析)
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- 从MySQL数据库迁移到AWS DynamoDB
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- A new method for evaluating the quality of metagenome assembly - magista
- 洛谷P1858 【多人背包】 (背包求前k优解)
- 2020 is coming to an end, special and unforgettable.
- 国外LEAD,联盟经理常见问答
- Redis 命令大全
- MYSQL查询至少连续n天登录的用户
猜你喜欢
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Bacterial infection and antibiotic use
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
Chapter 4 - understanding standard equipment documents, filters and pipelines
MYSQL50道基础练习题
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
智能电子秤全国产化电子元件推荐方案
[BIM introduction practice] wall hierarchy and FAQ in Revit
STM32 MCU ADC rule group multi-channel conversion DMA mode
383. Ransom letter
随机推荐
Supplement 14: cmake practice project notes (to be continued 4 / 22)
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
协程与多进程的完美结合
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
test
MYSQL50道基础练习题
The perfect combination of collaborative process and multi process
STM32上μC/Shell移植与应用
C语言常用字符串处理函数
Error occurs when thymeleaf th: value is null
win10, mysql-8.0.26-winx64. Zip installation
第四章 --- 了解标准设备文件、过滤器和管道
win10, mysql-8.0.26-winx64.zip 安装
递归调用--排列的穷举
Installation and use of Apache bench (AB pressure test tool)
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Effects of antibiotics on microbiome and human health
zynq平台交叉编译器的安装
Improving 3D object detection with channel wise transformer
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩