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. 添加app第一次启动页面

    一.添加几个成员变量 @interface hDisplayView ()<UIScrollViewDelegate> { UIScrollView *_bigScrollView; NS ...

  2. python2.0_day19_前端分页功能的实现

    我们前面完成的客户纪录展示,只有4条,如果有上百条就不能在1页中全部展示了,那样是不人性化的.另外一次性取出来,数据量也比较大.假如现在有95条数据,我们想实现一个每页展示20条,那就分为5页.假如我 ...

  3. Python 入门(四)List和Tuple类型

    创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...

  4. oct()

    oct() 用于将一个十进制的整数转换成八进制字符串 In [10]: oct(10) Out[10]: ' In [11]: oct(20) Out[11]: '

  5. HTTP 请求过程

    如下,我们在浏览器输入一个域名,按回车之后,便向该域名对应的服务器发送了一个请求:服务器接收到这个请求后进行处理和解析,然后返回响应的内容给浏览器,浏览器再对其进行解析然后呈现网页 我们可以通过 Ch ...

  6. RegisterHotKey注册快捷键

    RegisterHotKey的具体使用方使用方法如下: BOOL   RegisterHotKey( HWND   hWnd,         //响应该热键的窗口句柄 Int   id,       ...

  7. PyQt4文件对话框QFileDialog

    文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...

  8. Oracle分页查询sql语句

    1. select * from ( select  t.*, rownum RN from TABLE_NAME  t ) where RN > 0 and RN <= 15 2. se ...

  9. tomcat日志保存在哪里?

    tomcat目录下的logs文件夹下.有个catalina.20xx-xx-xx.log

  10. 在lampp的proftpd下新增FTP用户的方法与配置

    用LAMPP的安装方法可以开一个默认的lampp用户,不过多用户怎样管理.目录怎样设置?这里简明说一下. 要求:使用Lampp的proftpd,开通多个FTP用户,并各分配一个目录,而且需要限制用户在 ...