题目:

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. (Medium)

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 这一条件的位置。

注意如果最后一个元素也不大于的情况。

代码:

 class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (nums.size() == ) {
return -;
}
int start = , end = nums.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] < target) {
start = mid;
}
else {
end = mid;
}
}
if (nums[start] >= target) {
return start;
}
if (nums[end] >= target) {
return end;
}
return end + ;
}
};
 

LeetCode35 Search Insert Position的更多相关文章

  1. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  2. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

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

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

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

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

  6. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  7. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  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. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

随机推荐

  1. Supports BorlandIDEServices

    Delphi: procedure SetKeystrokeDebugging(Debugging: Boolean); var Dialog: IOTAKeyboardDiagnostics beg ...

  2. 转】MyEclipse使用总结——设置MyEclipse开发项目时使用的JDK

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3927677.html 感谢! 安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyE ...

  3. UVALive 5881 Unique Encryption Keys (DP)

    Unique Encryption Keys 题目链接: http://acm.hust.edu.cn/vjudge/problem/26633 Description http://7xjob4.c ...

  4. 《面向对象程序设计》第二次作业(1)(A+B问题)

    作业记录: 问题描述与代码已上传github仓库object-oriented文件夹下 题目一览 Calculate a + b and output the sum in standard form ...

  5. stl map高效遍历删除的方法 [转]

    for(:iter!=mapStudent.end():) {      if((iter->second)>=aa)      {          //满足删除条件,删除当前结点,并指 ...

  6. HTTP(一) 连接管理

    ・HTTP是如何使用TCP连接的 HTTP传送一条报文时,以流的形式将报文数据内容通过一条打开的TCP连接按序传输. TCP收到数据流之后,由TCP/IP软件将数据流砍成被称作段的小数据块,并将段封装 ...

  7. 关于新建JSP文件后,文件开头报错的处理

    新建了一个web工程,之后建立了jsp页面,刚建立完成,文件开头就报错:The superclass "javax.servlet.http.HttpServlet" was no ...

  8. glibc strlen delphi pascal

    From: Will DeWitt Jr. Subject: Fast strlen routine? NewsGroup: borland.public.delphi.language.basm D ...

  9. ARM&Linux 下驱动开发第一节(小试牛刀)

    #include<linux/init.h> #include<linux/module.h> static int __init hello_init(void) { pri ...

  10. C# 多线程处理相关说明: WaitHandle,waitCallback, ThreadPool.QueueUserWorkItem

    class TestThread { static void Main() { //使用WaitHandle静态方法阻止一个线程,直到一个或多个同步对象接收到信号 WaitHandle[] waitH ...