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. 运行 3ds Max 时出现的性能问题

          运行 3ds Max 时性能减慢或迟缓通常是由于视频配置冲突或内存分配问题引起的.关于性能问题的一大难点在于缩小范围确定问题原因.以下是一些限制 3ds Max 操作的常见情形,以及纠正这 ...

  2. IIC协议

    总线信号 :  SDA :串行数据线 SCL  :串行时钟 总线空闲状态 : SDA :高电平 SCL :高电平 起始位:SCL为高电平期间    SDA出现下降沿 终止位:SCL为高电平期间 SDA ...

  3. CentOS 6.3下Samba服务器的安装与配置详解

    一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的 ...

  4. WPS Word查询某些内容的出现次数

    1.CTRL+F 打开查找窗体

  5. MYSQL系列之(一)

      mysql简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不 ...

  6. javascript关于链接的一些用法

    (1)javascript:openURL() http://www.kpdown.com/search?name=Ben Nadel 此URL后边有一个name参数,只不过参数的值竟然带了空格,这样 ...

  7. 【BZOJ3262】陌上花开 cdq分治

    [BZOJ3262]陌上花开 Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义 ...

  8. Mac - 关闭隐藏文件显示(Terminal)

    打开终端Terminal,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write ...

  9. OC开发_Storyboard——UIApplication和网络活动指示器

    一.UIApplication 只有一个实例: UIApplication *myApplication = [UIApplication sharedApplication]; 属性如果设置为YES ...

  10. python下几种打开文件的方式

    昨天看完了这本python进阶,感觉这本书对我启发很大,做了三张纸的笔记,方便我在遇到问题的时候翻阅,然后寻找可能的解决方案.作为一个使用Python一年的小白,虽然说不是小白,但是这一年来基本上是用 ...