当前位置:网站首页>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
边栏推荐
- /etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
- Huawei machine test -- high precision integer addition
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- 协程与多进程的完美结合
- 华为机试--高精度整数加法
- 229. 求众数 II
- 阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
- [AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- Basic use of shell WC (counting the number of characters)
猜你喜欢
Go reflection rule
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
test
Installation du compilateur croisé de la plateforme zynq
【Echart】echart 入門
无线键盘全国产化电子元件推荐方案
MySQL queries users logged in for at least N consecutive days
229. 求众数 II
补:注解(Annotation)
随机推荐
Leetcode - > 1 sum of two numbers
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
VHDL implementation of 32-bit binary to BCD code
上海航芯技术分享 | ACM32 MCU安全特性概述
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Record the blind injection script
Chlamydia infection -- causes, symptoms, treatment and Prevention
重剑无锋,大巧不工
win10, mysql-8.0.26-winx64. Zip installation
Phishing for NFT
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Migrate from MySQL database to AWS dynamodb
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
[pytoch foundation] torch Split() usage
MYSQL查询至少连续n天登录的用户
win10, mysql-8.0.26-winx64.zip 安装
Understand the gut organ axis, good gut and good health
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Go 语言中的 logger 和 zap 日志库
Redis command Encyclopedia