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. 静态变量数组实现LRU算法

    LRU算法的解释详情请见 https://baike.baidu.com/item/LRU/1269842 这里百度百科给出的比较详细,然后后面有一个例子 说 LRU(least recently u ...

  2. scala的基础部分

    最近接触到spark,spark又是scala编写的,所以需要学习一下scala. scala是面向对象的,一切皆为对象, 数值,函数都是对象. println("Welcome to th ...

  3. java日志之log4j简单使用

    1.导入包log4j.jar 2.src同级创建并设置log4j.properties ### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控 ...

  4. python文件和目录操作方法大全(含实例)【python】

    转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Py ...

  5. 系统启动日志:/var/log/boot.log

    /var/log/boot.log — 记录系统启动时的日志信息,如果系统启动之后有什么异常可以查看该文件信息 [root@localhost ~]# cat /var/log/boot.log # ...

  6. docker tag 详解

    docker tag 用于给镜像打标签,语法如下: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] ① 比如我现在有一个 centos 镜像: [ro ...

  7. windows下配置nutch注意的问题

    1.为处理方便,直接在$nutch目录下创建一个名为url.txt文件,然后在文件里添加要搜索的网址,例如:http://www.sina.com.cn/,注意网址最后的"/"一定 ...

  8. Objective-C中的KVC与KVO(上)

    本文转载 李朴之先生博客 http://blog.csdn.net/pucker/article/details/7413280 Objective-C中的KVC与KVO是两种比较重要的技术,这里简要 ...

  9. C++说明符和限定符

    有些被称为存储说明符(storage class specifier)或cv-限定符(cv-qualifier)的C++关键字提供了一些有关存储的信息.下面是存储所说明符:* auto (在C++11 ...

  10. Java自动类型转换

    ■ 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型. ■ 特例:可以讲整型常量直接赋给byte,short,char等类型变量,而不需要强制类型转换,只要不超出其表数范围. ■ 强制类型 ...