34 Search for a Range(目标数的范围Medium)
题目意思:递增数组,找到目标数的范围,找不到则返回[-1,-1]
思路:折半查找
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int start=,end=nums.size()-;
vector<int> ans;
while(start<=end){
int temp=(start+end)/;
if(nums[temp]>target){
end=temp-;
}
else if(nums[temp]<target){
start=temp+;
}
else if(nums[temp]==target){
int flag1=temp,flag2=temp;
while(flag1>&&nums[flag1-]==target){
--flag1;
}
while(flag2<nums.size()-&&nums[flag2+]==target){
++flag2;
}
ans.push_back(flag1);
ans.push_back(flag2);
return ans;
}
}
ans.push_back(-);
ans.push_back(-);
return ans;
}
};
时间复杂度:O(logn)
运行时间:12ms
代码太冗余了,以后再精简吧
34 Search for a Range(目标数的范围Medium)的更多相关文章
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [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] 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 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 34. Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- cf703A Mishka and Game
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- C++使用模版技术将任意类型的数据转为某个类型的数据
将任意类型(int, float, 自定义的数据类型等等)的数据转换的某个类型C中储存,可以通过 将类型C的构造函数写成模版函数的形式,在C中将可以接收任意类型数据.如: class C{ templ ...
- [Hibernate] List 映射例子
List 是 java 集合的一个工具,存储线性的数据,允许重复数据.开发者可以准确控制在 list 那个位置插入数据.本例子演示 Java 的 List 集合和 MySQL 数据库的映射应用. 使用 ...
- Struts2学习笔记(三):result配置的各项视图转发类型
Struts 1: <action path="/user" type="org.sunny.user.action.UserAction" ...> ...
- Java并发编程:线程间通信wait、notify
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- UIScreen UIWindow UIView
UIScreen(屏幕),UIWindow(窗口),UIView(视图)是IOS的几个基本界面元素.其中UIWindow(窗口)和UIView(视图)是为iPhone应用程序构造用户界面的可视组件.U ...
- [PWA] 14. Loop cursor
import idb from 'idb'; var dbPromise = idb.open('test-db', 4, function (upgradeDb) { switch (upgrade ...
- [AngularJS] Use ng-model-options to limit $digest
Refer: http://toddmotto.com/super-fast-angular-ng-model-options-limit-digest-cycles/ Use: <input ...
- 定制属于自己的Chrome起始页
个人感觉没什么技术含量,可是很有用.我定制的起始页面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ19saWNodW4=/font/5 ...
- 使用EPEL和REMI第三方yum源
http://dl.fedoraproject.org/pub/epel/ epel-release-latest-.noarch.rpm redhat5 epel-release-latest-.n ...