【LeetCode】34. Search for a Range
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]
.
由于O(logn)时间要求,显然用二分查找。
思路是先用二分查找找到其中一个target,找不到则返回默认的ret值[-1, -1]
找到之后从这个位置往两边递归进行二分查找进行范围的拓展。
具体来说,ret[0]不断向左扩展,ret[1]不断向右扩展。
- class Solution {
- public:
- vector<int> searchRange(int A[], int n, int target) {
- vector<int> ret(, -);
- int left;
- int right;
- int low = ;
- int high = n-;
- while((left = binarySearch(A, low, high, target)) != -)
- {
- ret[] = left;
- high = left-;
- }
- low = ;
- high = n-;
- while((right = binarySearch(A, low, high, target)) != -)
- {
- ret[] = right;
- low = right+;
- }
- return ret;
- }
- int binarySearch(int A[], int left, int right, int target)
- {
- while(left <= right)
- {
- int mid = left + (right-left) / ;
- if(A[mid] == target)
- return mid;
- else if(A[mid] < target)
- left = mid + ;
- else
- right = mid - ;
- }
- return -;
- }
- };
【LeetCode】34. Search for a Range的更多相关文章
- 【一天一道LeetCode】#34. Search for a Range
一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- LeetCode OJ 34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
随机推荐
- 四边形优化dp
理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...
- iOS:UIScrollView控件和UIPageControl控件的详解
UIScrollView滚动视图控件和UIPageControl分页视图控件: UIScrollView用于显示多于一个屏幕的内容,超出屏幕范围的内容可以通过滑动进行查看,当然UIPagecon ...
- C语言存储类型
看c专家编程,有说存储类型一直不太清楚.看到一篇文章讲解c的存储类型,讲解了c语言中的各种变量的存储类型,而且是从进程.内存的角度讲解的,以前从没有这样理解过,觉得挺有用的,在这里转载过来. 首先要来 ...
- [leetcode]Flatten Binary Tree to Linked List @ Python
原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...
- $.jsonp()的简单使用
// jsonp 获取 json 数据: $.jsonp({ url: GLOBAL.baseUrl + '/company/mobi_getposter.action', callback: 'ca ...
- 深入理解this和call、bind、apply对this的影响及用法
首先看一道网易的面试题: var a = { a:"haha", getA:function(){ console.log(this.a); } } var b = { a:&qu ...
- (算法)前K大的和
题目: 1.有两个数组A和B,每个数组有k个数,从两个数组中各取一个数加起来可以组成k*k个和,求这些和中的前k大. 2.有N个数组,每个数组有k个数,从N个数组中各取一个数加起来可以组成k^N个和, ...
- 高并发分布式环境中获取全局唯一ID[分布式数据库全局唯一主键生成]
需求说明 在过去单机系统中,生成唯一ID比较简单,可以使用MySQL的自增主键或者Oracle中的sequence, 在现在的大型高并发分布式系统中,以上策略就会有问题了,因为不同的数据库会部署到不同 ...
- Javascript高级程序设计-对象
学习Javascript,最难的地方是什么? Object(对象)最难,初学者不容易掌握. Javascript是一种基于对象(object-based)的语言,它的语法中没有class(类). C# ...
- ZH奶酪:JavaScript中的JSON.stringify() and JSON.parse()
JSON.stringify() JSON.stringify()可以将任意的JavaScript值序列化成JSON字符串. 语法 JSON.stringify(value[, replacer [, ...