Search Insert Position

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

这就是普通的二分查找,需要注意的地方代码注释中都有。其中特别注意的是:middle=(low+high)/ 2 ,可能会溢出,可以替换为middle = low + ((high - low)>>2);

class Solution {
public:
int searchInsert(int A[], int n, int target) {
//二分查找
int low = 0, high = n-1;//注意这里high是初始化为n-1
while(low <= high)//注意这里是<=号
{
//int middle = (low + high) / 2;
int middle = low + ((high - low)>>2); //可以防止low + high 溢出
if(A[middle] < target)
low = middle + 1;
else if(A[middle] > target)
high = middle - 1;
else return middle;
}
return low;//注意返回值是low
}
};

根据程序员编程艺术第二十五章:Jon Bentley:90%无法正确实现二分查找, 如果high初始化为n, 那么while判断语句就应该是low<high, 下面的A[middle] > target分支中,就应该是high = middle


Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

看到这个有没有想到STL中的equal_range函数,这个函数是调用lower_boundupper_bound, 下面我们仿照STL的实现。相比上一题的二分查找,lower_bound当A[middle] == target时,继续向左半部分查找,它返回的是第一个不小于目标数的元素位置;upper_bound当A[middle] == target时,继续向右半部分查找,它返回的是第一个大于目标数的元素位置。    本文地址

class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> res(2, -1);
int low = 0, high = n-1;
while(low <= high)
{
int middle = low + ((high - low)>>2);
if(A[middle] < target)
low = middle + 1;
else if(A[middle] > target)
high = middle - 1;
else
{
res[0] = lowerBound(A, low, middle - 1, target);
res[1] = upperBound(A, middle + 1, high, target) - 1;
return res;
}
}
return res;
} //找到范围内[left,right]内第一个不小于target的元素
int lowerBound(int A[], int left, int right, int target)
{
int low = left, high = right;
while(low <= high)
{
int middle = low + ((high - low)>>2);
if(A[middle] < target)
low = middle + 1;
else high = middle - 1;
}
return high + 1;//注意这里返回值不是low
}
//找到范围[left,right]内第一个大于target的元素
int upperBound(int A[], int left, int right, int target)
{
int low = left, high = right;
while(low <= high)
{
int middle = low + ((high - low)>>2);
if(A[middle] <= target)
low = middle + 1;
else high = middle - 1;
}
return low;
}
};

【版权声明】转载请注明出处http://www.cnblogs.com/TenosDoIt/p/3681677.html

LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)的更多相关文章

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

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

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

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

  3. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  4. Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)

    Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...

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

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

  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: 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. LeetCode 2 Add Two Numbers(链表操作)

    题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...

  2. Effective Java 23 Don't use raw types in new code

    Generic types advantage Parameterized type can provide erroneous check in compile time. // Parameter ...

  3. Android开发之 Android应用程序目录结构解析

    建立的HelloWorld的应用项目,其代码是由ADT插件自动生成的,形成Android项目特有的结构框架. 接下来让我带领大家解析一个Android程序的各个组成部分,这次我们拿一个Hello,Wo ...

  4. nyoj 38 布线问题

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=38 最小生成树水题~ 代码: #include "stdio.h" / ...

  5. private成员变量真的私有吗?(用指针刨他祖坟)

    今天写程序时突然想到的,为什么不用指针去获取类的成员变量呢.于是做了这个实验.首先定义了一个类: class Test { private: int i; char c; int* p; public ...

  6. [麦先生]LINUX常用命令总结

    在系统的学习了如何搭建和利用LINUX进行开发后,我利用xMind这一个强大的bug级软件制作了LINUX常见操作命令汇总,但是由于博客园并不支持xMind格式文件的上传,我只能将其做成图片进行分解上 ...

  7. 最小瓶颈路 Uva 534 Frogger

    说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做. 最小瓶颈路:找u到v的一条路径满足最大边权值尽量小 先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这 ...

  8. ZBrush中如何才能快速完成脸部雕刻(下)

      骨骼,是一门基础艺术,几百年来一直为伟大的艺术大师所研究,它曾经,也将一直是创作现实且可信角色的关键,提高骨骼知识更将大大提高雕刻技能. 查看更多内容请直接前往:http://www.zbrush ...

  9. 如何解决inline-block元素的空白间距

    早上在博客中有人提了这样一个问题:“li元素inline-block横向排列,出现了未知间隙”,我相信大家在写页面的时候都遇到过这样的情况吧. 我一般遇到这情况都会把li浮动起来,这样就没有间隙.但是 ...

  10. EDM总结

    1.  使用table 布局,align="center" 2.  尽量不要用padding-left ,padding-right 样式: 3.  CSS的浮动定位: 4.  不 ...