当前位置:网站首页>Leetcode009 -- search the target value in the array with binary search
Leetcode009 -- search the target value in the array with binary search
2022-04-23 04:39: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 {
/**
*
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Example 2:
Input : nums = [1,3,5,6], target = 2
Output : 1
Example 3:
Input : nums = [1,3,5,6], target = 7
Output : 4
Example 4:
Input : nums = [1,3,5,6], target = 0
Output : 0
Example 5:
Input : nums = [1], target = 0
Output : 0
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums Arrange the array in ascending order without repeating elements
-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://yzsam.com/2022/04/202204230438035243.html
边栏推荐
- IEEE Transactions on Industrial Informatics(TII)投稿须知
- Chlamydia infection -- causes, symptoms, treatment and Prevention
- Jetpack 之 LifeCycle 组件使用详解
- Understand the gut organ axis, good gut and good health
- The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
- Installation and use of Apache bench (AB pressure test tool)
- What is the thirty-six plan
- Recommended scheme for national production of electronic components of wireless keyboard
- Apache Bench(ab 压力测试工具)的安装与使用
- Go反射—Go语言圣经学习笔记
猜你喜欢
Introduction to Cortex-M3 register set, assembly language and C language interface
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
zynq平台交叉编译器的安装
C语言:恶搞小游戏
MYSQL50道基础练习题
针对NFT的网络钓鱼
520. Detect capital letters
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Use recyclerview to realize left-right side-by-side classification selection
无线键盘全国产化电子元件推荐方案
随机推荐
Recommended scheme of national manufactured electronic components
229. 求众数 II
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Logger and zap log Library in go language
thymeleaf th:value 为null时报错问题
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Summary of MySQL de duplication methods
电钻、电锤、电镐的区别
补:注解(Annotation)
Chapter 4 - understanding standard equipment documents, filters and pipelines
Why recommend you to study embedded
做数据可视化应该避免的8个误区
AWS EKS 部署要点以及控制台与eksctl创建的差异
The perfect combination of collaborative process and multi process
Basic use of shell WC (counting the number of characters)
无线充电全国产化电子元件推荐方案
IEEE Transactions on Industrial Informatics(TII)投稿须知
shell wc (统计字符数量)的基本使用
383. Ransom letter