【leetcode】Search for a Range(middle)
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].
思路:
我自己用了个最简单的,普通的二分查找,然后两边延伸找起始和结束点。
int *searchRange(int A[], int n, int target) {
int ans[] = {-, -};
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(A[m] == target)
{
ans[] = ans[] = m;
while(ans[] - >= && A[ans[]] == A[ans[] - ]) ans[]--;
while(ans[] + < n && A[ans[]] == A[ans[] + ]) ans[]++;
return ans;
}
else if(A[m] > target)
r = m - ;
else
l = m + ;
}
return ans;
}
大神分成了分别用两次二分查找找起始和结束位置
https://leetcode.com/discuss/18242/clean-iterative-solution-binary-searches-with-explanation中有详细解释
vector<int> searchRange(int A[], int n, int target) {
int i = , j = n - ;
vector<int> ret(, -);
// Search for the left one
while (i < j)
{
int mid = (i + j) /;
if (A[mid] < target) i = mid + ;
else j = mid;
}
if (A[i]!=target) return ret;
else ret[] = i;
// Search for the right one
j = n-; // We don't have to set i to 0 the second time.
while (i < j)
{
int mid = (i + j) / + ; // Make mid biased to the right
if (A[mid] > target) j = mid - ;
else i = mid; // So that this won't make the search range stuck.
}
ret[] = j;
return ret;
}
另一种通过加减0.5来找位置的方法
class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return a list of length 2, [index1, index2]
def searchRange(self, arr, target):
start = self.binary_search(arr, target-0.5)
if arr[start] != target:
return [-1, -1]
arr.append(0)
end = self.binary_search(arr, target+0.5)-1
return [start, end] def binary_search(self, arr, target):
start, end = 0, len(arr)-1
while start < end:
mid = (start+end)//2
if target < arr[mid]:
end = mid
else:
start = mid+1
return start
【leetcode】Search for a Range(middle)的更多相关文章
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
随机推荐
- 优酷土豆2014校园招聘笔试题目之Java开发类
先总体说下题型,共有20道选择题,4道简答题,3道编程题和1道扩展题,题目都比较简单,限时一小时完成. 一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插 ...
- Google翻译请求(难点是tk参数)
业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...
- Outlook不能打开附件(提示:无法创建文件xx,请右键单击要在其中创建文件的文件夹..)
问题分析: 出现这种问题的几率很小,除非你是每天都需要使用Outlook的办公人员.出现这种问题我想有如下两种可能.1.注册表中指定的附档临时保存的目录没有写入的相关权限.2.同名附档已存在且权限出现 ...
- 繁华模拟赛day8 牛栏
/* 标称并没有用到题解中提到的那种奇妙的性质,我们可以证明,正常从1开始走的话,需要T次,如何使这个次数减小?题解中提到一个办法,有一步小于n/t,我们考虑这一步,如果把它匀到左右两步中,则可以减小 ...
- Spark之命令
Spark之命令 1.spark运行模式有4种: a.local 多有用测试, b. standalone:spark 集群模式,使用spark自己的调度方式. c. Yarn: 对Mapreduce ...
- 【PHP面向对象(OOP)编程入门教程】12.重载新的方法(parent::)
在学习PHP 这种语言中你会发现, PHP中的方法是不能重载的, 所谓的方法重载就是定义相同的方法名,通过“参数的个数“不同或“参数的类型“不 同,来访问我们的相同方法名的不同方法.但是因为PHP是弱 ...
- 【C语言入门教程】2.9 小结
本章介绍 C 语言的基本组成部分,数据类型.运算符 和 表达式 构成了 C 语言的语法,熟悉和掌握这些信息是学习 C 语言的必经之路.C 语言具备严谨的语法结构,任何细微的差错可导致程序无法通过编译, ...
- 巧用jQuery选择器写表单办法总结(提高效率)
转载自:http://blog.csdn.net/violetjack0808/article/details/52221343 1.文本和文本框 <!DOCTYPE html> < ...
- 服务器&域名那些事儿
购买的阿里云的服务器(ECS)和域名 请移步: 服务器&域名那些事儿 服务器&域名那些事儿2 github 博客
- Java Io 流(输入输出流)
IO流,也就是输入和输出流,可分为字节流和字符流. 1. 字节流 (1). InputStream 输入流,用于读取文件 输入流常用API: inputStream.read() 读取一个字节 in ...