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.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

 思路:排好序的数列用二分法进行搜索。二分法查找到的最后那个元素,要么是target,要么是紧邻target左侧或右侧的元素。
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.size()==) return ;
return binarySearch(nums,,nums.size()-,target);
} int binarySearch(vector<int>& nums, int start, int end, int target){
if(start==end){
if(target <= nums[start]) return start;
else return start+;
} int mid = start + ((end-start)>>);
if(target <= nums[mid]) return binarySearch(nums,start,mid,target);
else return binarySearch(nums,mid+,end,target);
}
};

35. Search Insert Position (Array; Divide-and-Conquer)的更多相关文章

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

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

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

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

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

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

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

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

  6. 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 ...

  7. [LeetCode] 35. Search Insert Position 搜索插入位置

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

  8. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  9. [LeetCode] 35. Search Insert Position 解决思路

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

随机推荐

  1. ballerina 学习十三 函数&&documentation

    ballerina 函数和其他语言一样的,可以实现重用 简单例子 代码 import ballerina/io; documentation { `User` is a user defined ob ...

  2. WinForm 窗体初始位置篇

    1.在C#中,From本身有个StartPosition属性可以控制居中显示. StartPosition 默认值是WindowsDefaultLocation ,我们只需要改成CenterScree ...

  3. poj 1637 Sightseeing tour——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

  4. Mongodb简单介绍安装

    具体详细内容,请查阅 Mongodb官方文档 一.简单介绍 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. M ...

  5. CSS基本样式简单介绍

    具体详情内容请查阅<css参考手册> 一.基本结构样式 width 宽度 height 高度 background 背景 border 边框 padding 内边距 margin 外边距 ...

  6. Ubuntu下手动安装Nvidia显卡驱动

    1. 下载最新版的nVidia驱动. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo gedit /etc/modpr ...

  7. 适配:px与dp转换

    public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context con ...

  8. spring-session之四:Spring Session下的Redis存储结构

    spring-session项目启动后 127.0.0.1:6379> keys * 1) "spring:session:index:org.springframework.sess ...

  9. 如何搭建struts2框架

    一.首先,下载5个Struts2核心jar包: commons-logging-1.1.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core- ...

  10. Go - 开始

    学习Go的缘由 由于LZ目前在使用docker,docker的编程语言使用的是“Go”,所以想更加深入的了解Docker(尝试着看懂source code)遂尝试了解下Golang. 安装 LZ用的是 ...