当前位置:网站首页>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
边栏推荐
- Go反射—Go语言圣经学习笔记
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
- IEEE Transactions on industrial information (TII)
- 针对NFT的网络钓鱼
- HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- [AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
- 2021数学建模国赛一等奖经验总结与分享
- Inverse system of RC low pass filter
猜你喜欢
![[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022](/img/40/72fdf9c89ed7d063cc368e6e052d0f.png)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022

Bacterial infection and antibiotic use

補:注解(Annotation)

Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library

AWS eks add cluster user or Iam role

Installation du compilateur croisé de la plateforme zynq

229. Find mode II
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

Supplement 14: cmake practice project notes (to be continued 4 / 22)

无线键盘全国产化电子元件推荐方案
随机推荐
Interaction of diet gut microbiota on cardiovascular disease
leetcode004--罗马数字转整数
Installation du compilateur croisé de la plateforme zynq
Introduction to Cortex-M3 register set, assembly language and C language interface
[echart] Introduction to echart
Go reflection - go language Bible learning notes
Supplement: Annotation
2021数学建模国赛一等奖经验总结与分享
A new method for evaluating the quality of metagenome assembly - magista
Bacterial infection and antibiotic use
Installation and use of Apache bench (AB pressure test tool)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
MYSQL查询至少连续n天登录的用户
Jetpack 之 LifeCycle 组件使用详解
【Echart】echart 入門
zynq平臺交叉編譯器的安裝
Migrate from MySQL database to AWS dynamodb
Huawei machine test -- high precision integer addition
test
io.Platform.packageRoot; // ignore: deprecated_member_use