leetcode34——Search for a Range

原创 2018年04月16日 15:02:34

题目大意:在给出的升序数组中找出目标数字的起始和终止下标

分析:二分搜索。不过发现普通遍历也挺快的。

代码:

方法一:普通遍历

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans;
int start = -1, end = -1;
for (int i = 0;i < nums.size();i++) {
if (nums[i] == target) {
end = i;
if (start == -1)
start = i;
}
else if (nums[i] > target) {
ans.push_back(start);
ans.push_back(end);
return ans;
}
}
ans.push_back(start);
ans.push_back(end);
return ans;
}

};

方法二:二分搜索。转载自https://blog.csdn.net/booirror/article/details/50933805

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
int begin = 0;
int end = nums.size()-1;
vector<int> vs;
while (begin <= end) {
int mid = (begin + end) / 2;
if (nums[mid] == target) {
int min = mid;
while (min >= 0 && nums[mid] == nums[min])
min--;
int max = mid;
while (max <= end && nums[mid] == nums[max]) {
max++;
}
vs.push_back(min+1);
vs.push_back(max - 1);
return vs;
}
else if (nums[mid] > target) {
end = mid - 1;
}
else {
begin = mid + 1;
}
}
vs.push_back(-1);
vs.push_back(-1);
return vs;
}
};


CSA课程:A段架构师的职责

传授CSA等级的架构设计思想、方法与技术。让想深度领会架构设计幕后的思想者,和盼望早日成为CSA者,都能孰悉A段架构师的角色。
  • 2015年09月22日 14:37

[LeetCode34]Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Yo...
  • sbitswc
  • sbitswc
  • 2014-06-16 01:31:31
  • 5748

LeetCode 34 — Search for a Range(C++ Java Python)

题目: 题目翻译: 分析: C++实现: Java实现: Python实现: 感谢阅读,欢迎评论!...
  • dragon_dream
  • dragon_dream
  • 2014-04-03 20:53:45
  • 3541

LeetCode-34-Search for a Range Python实现lower_bound

手写 lower_bound class Solution(object): def searchRange(self, nums, target): """ ...
  • qdbszsj
  • qdbszsj
  • 2017-09-12 21:29:03
  • 234

LeetCode34——Search for a Range

LeetCode34——Search for a Range 题意: 在有序序列中找到重复出现的数字出现的范围,返回一个二元组代表范围的闭区间,如果找不到target则返回[-1,-1] ...
  • zhangxiao93
  • zhangxiao93
  • 2015-10-15 21:54:10
  • 519

LeetCode34: Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. You...
  • geekmanong
  • geekmanong
  • 2016-01-13 15:57:11
  • 1194

Leetcode34 Search for a Range

Search for a Range Given a sorted array of integers, find the starting and ending position of a gi...
  • u011513853
  • u011513853
  • 2015-07-28 18:34:48
  • 183

python实现 LeetCode34——Search for a Range

二分法找到target的位置,再判断前后的位置class Solution(object): def searchRange(self, nums, target): star...
  • xiaoling_000666
  • xiaoling_000666
  • 2018-03-21 20:11:12
  • 6

LeetCode解题报告--Search for a Range

题目: Given a sorted array of integers, find the starting and ending position of a given target valu...
  • github_27609763
  • github_27609763
  • 2015-12-04 13:32:03
  • 1767

Search for a Range -- LeetCode

原题链接: http://oj.leetcode.com/problems/search-for-a-range/  这道题是二分查找Search Insert Position的变体,思路并不复杂...
  • linhuanmars
  • linhuanmars
  • 2014-03-06 03:48:49
  • 17567
收藏助手
不良信息举报
您举报文章:leetcode34——Search for a Range
举报原因:
原因补充:

(最多只允许输入30个字)