[LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
035. Search for a Range (Medium)
链接:
题目:https://leetcode.com/problems/search-for-a-range/
代码(github):https://github.com/illuz/leetcode
题意:
在有序数组中找到一个数的范围。(由于数有反复)
分析:
还是二分搜索变形。
- (C++)直接用 C++ STL 的
lower_bound
和upper_bound
偷懒。 - (Java)直接从普通的二分改一下即可了。
代码:
C++:
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
int* lower = lower_bound(A, A + n, target);
int* upper = upper_bound(A, A + n, target);
if (*lower != target)
return vector<int> {-1, -1};
else
return vector<int>{lower - A, upper - A - 1};
}
};
Java:
public class Solution { public int[] searchRange(int[] A, int target) {
int[] ret = new int[2];
ret[0] = ret[1] = -1;
int left = 0, right = A.length - 1, mid; while (left <= right) {
if (A[left] == target && A[right] == target) {
ret[0] = left;
ret[1] = right;
break;
} mid = (right + left) / 2;
if (A[mid] < target) {
left = mid + 1;
} else if (A[mid] > target) {
right = mid - 1;
} else {
if (A[right] == target) {
++left;
} else {
--right;
}
}
} return ret;
}
}
[LeetCode] 034. Search for a Range (Medium) (C++/Java)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- LeetCode 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
- Java for LeetCode 034 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 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 【leetcode】Search for a Range(middle)
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 an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- leetcode 【 Search for a Range 】python 实现
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- 无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟
同事说,有个语句5个小时不出结果,叫我帮忙看看,于是叫同事发过来.不看不知道,一看吓一跳,3个表关联,强制使用了2个index hint,当中一个表9g,一个表67g,另一个小表40Mb.开发者,总以 ...
- CPU使用率和Load Average的关系
看了几篇博客总结的区别,自己终于明白了含义,在这里将理解总结一下: 对于定义和解释,感觉淘测试上的更容易理解: 引用如下: CPU使用率: 一段时间内CPU的使用状况,从这个指标可以看出某一段时间内 ...
- DotNet程序汉化过程--SnippetCompiler准确定位
开篇前言 上一篇简单介绍了一下怎么汉化.Net程序,但那也仅仅是最基础的工作,要想汉化好一款软件基础我们得做扎实了,但是对于一些需要技巧的也不能不会啊,这一篇就介绍一下怎么准确定位字符串. 主要使用工 ...
- DropDownList绑定数据
DDLName.DataSource = myRd;DDLName.DataTextField = "name";//要绑定的字段DDLName.DataValueField = ...
- ajax例子
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- .NET中应用Ueditor(富文本编辑)的配置和使用
一.Ueditor的下载 1.百度编辑器下载地址:http://ueditor.baidu.com/website/download.html 2.下载完整源码包,解压到任意目录,解压后的源码目录结构 ...
- html5新特性--音频视频,拖放
1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...
- struts2 MessageStoreInterceptor 拦截器的使用
MessageStoreInterceptor 拦截器可以把和该 Action 相关的 messages, errors 和 field errors(下称 "消息") 保存到 s ...
- AttributeError at /home/home/ Exception Type: AttributeError at /home/home/
"错误提示信息": Environment: Request Method: GET Request URL: http://localhost:8000/home/home/ D ...
- Erlang中的图形化检测工具(4)
这儿例举出若干个用于检视运行时系统的图形化工具,这些工具可以很好地帮助我们增进对系统的理解.借助这些工具,我们可以很好地以图形化方式观察进程.应用和监督层级. (1) Appmon.Appmon 是用 ...