LeetCode_35. Search Insert Position
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 4:
Input: [1,3,5,6], 0
Output: 0
package leetcode.easy;
public class SearchInsertPosition {
@org.junit.Test
public void test() {
int[] nums1 = { 1, 3, 5, 6 };
int target1 = 5;
int[] nums2 = { 1, 3, 5, 6 };
int target2 = 2;
int[] nums3 = { 1, 3, 5, 6 };
int target3 = 7;
int[] nums4 = { 1, 3, 5, 6 };
int target4 = 0;
System.out.println(searchInsert(nums1, target1));
System.out.println(searchInsert(nums2, target2));
System.out.println(searchInsert(nums3, target3));
System.out.println(searchInsert(nums4, target4));
}
public int searchInsert(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
}
}
LeetCode_35. Search Insert Position的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- certbot更新错误
自动更新老是提示这个错误. root@vultr:~/certbot# ./certbot-auto Upgrading certbot-auto 0.29.1 to 0.34.2... Couldn ...
- python练习题(一)
背景: 和公司的二位同事一起学习python,本着共同学习.共同成长.资源共享的目标,然后从中学习,三人行必有我师 练习题更新中······ 题目: 输入一个值num,如果 num 大于 10,输出: ...
- matlab(3) Logistic Regression: 求cost 和gradient \ 求sigmoid的值
sigmoid.m文件 function g = sigmoid(z)%SIGMOID Compute sigmoid functoon% J = SIGMOID(z) computes the si ...
- 51 arm x86 的大小端记录
51 是大端模式 arm的cortex m 默认小端,可以设置大端 x86是小端 大端模式:低位字节存在高地址上,高位字节存在低地址上 小端模式:高位字节存在高地址上,低位字节存在低地址上
- Windows10-Neo4j安装问题及解决方案
暑假都过得差不多了才终于开始搭环境了 1.下载Neo4j Neo4j官网下载翻墙的话还可以 不翻墙的话下了好几次都下不下来 不用下载desktop,下载community server就可以了 2.下 ...
- jquery判断两次密码不一致
jquery检测输入密码两次不一样提示 输入密码: <input type="password" name="password1" id="pa ...
- python定义函数时的参数&调用函数时的传参
一.定义函数: 1.位置参数:直接定义参数 2.默认参数(或者关键字参数):参数名 = "默认值" 3.位置参数必须在默认参数之前 二.调用函数: 1.按位置传,直接写参数的值 2 ...
- [Dart] Capture and Handle Data Sequences with Streams in Dart
Streams represent a sequence of asynchronous events. Each event is either a data event, also called ...
- UEFI分区损坏重建指南
自从国庆假期发了这两篇博客后,我这个人就像是从博客园消失了一样,半个多月没更新..自从10月5号把UEFI分区删掉之后,我的电脑就因为没有引导,找不到系统而无法使用了.所以这篇博客,就分享一下我在这半 ...
- LaTex编译时出错:PK font *** could not be created
参考https://blog.csdn.net/dancing11/article/details/60978642 在用TeXworks (Miktex) 编译OSA投稿模板时,遇到错误PK fon ...