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
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
return bt_search(nums,target);
}
int bt_search(vector<int>& a ,int target) {
int low = 0;
int high = a.size() - 1;
while (low<=high) {
int mid = low + (high - low) / 2;
if (a[mid] < target) {
low = mid + 1;
} else if (target < a[mid]) {
high = mid - 1;
} else {
return mid;
}
}
return low ;
}
};

  

 class Solution {
public int searchInsert(int[] nums, int target) {
int n = nums.length;
int lo = 0;
int hi = n-1;
while(lo<=hi){
int mid = lo+(hi-lo)/2;
if(nums[mid]>target)
hi = mid - 1;
else if (nums[mid]<target)
lo = mid + 1;
else
return mid;
}
return lo;
}
}

35. Search Insert Position(二分查找)的更多相关文章

  1. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  2. LeetCode Search Insert Position (二分查找)

    题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  3. LeetCode 35 Search Insert Position(查找插入位置)

    题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description   在给定的有序数组中插入一个目标数字,求出插入 ...

  4. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  5. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  6. 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导致的溢 ...

  7. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  8. 【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 ...

  9. 35. Search Insert Position@python

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. swift - 之 UIColor使用自定义的RGB配色

    1.10进制颜色 UIColor(red: /, green: /, blue: /, alpha: 0.5) 2.16进制颜色 UIColor(red: , green: , blue: , alp ...

  2. JSP自定义标签rtexprvalue属性

    rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...

  3. 【jQuery系列之插件】jQuery插件---exselect实现联动

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  4. Android 跨进程调用忽略权限

    Framework层: @Override    public StackInfo getStackInfo(int stackId) {        final int callingUid = ...

  5. Rsync匿名访问漏洞

    前言 前两天总结了互联网或者说IT公司内网常见的漏洞,然后决定针对还没学习过不了解的漏洞进行学习了解,所以准备一一针对来研习,今天是第一篇,立一个Flag,争取今年搞定,为啥说的这么艰难,因为平时工作 ...

  6. WEB安全番外第六篇--关于通过记录渗透工具的Payload来总结和学习测试用例

    背景: 在WEB安全的学习过程中,了解过了原理之后,就是学习各种Payload,这里面蕴藏着丰富的知识含量,是在基本上覆盖了漏洞原理之后的进一步深入学习的必经之路.无理是Burpsuite还是Sqlm ...

  7. Ubuntu16.04安装Elasticsearch

    一.安装工作 wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add - sudo apt-g ...

  8. [css]演示:纯CSS实现的右侧底部简洁悬浮效果

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...

  9. 将web项目打成war包部署在tomcat步骤

    将web项目打成war包部署在tomcat步骤 1.将自己的项目打成war包. 2.将打包好的war复制到${tomcat.home}/webapps项目下. 3.在${tomcat.hom}/con ...

  10. CodeForeces 25E (kmp)

    E. Test time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputst ...