034 Search for a Range 搜索范围
给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果在数组中找不到目标,返回 [-1, -1]。
例如:
给出 [5, 7, 7, 8, 8, 10] 和目标值 8,
返回 [3, 4]。
详见:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
Java实现:
class Solution {
    public int[] searchRange(int[] nums, int target) {
        int n=nums.length;
        int[] result = { -1, -1 };
        if(n==0||nums==null){
            return result;
        }
        int left = 0;
		int right = n - 1;
		while (left <= right) {
			int mid = (left + right)>>1;
			if (nums[mid] > target) {
				right = mid - 1;
			} else if (nums[mid] < target) {
				left = mid + 1;
			} else {
				int index = mid;
				while (index >= 0 && nums[index] == target) {
					--index;
				}
                result[0]=index+1;
				index = mid;
				while (index < nums.length && nums[index] == target) {
                    ++index;
				}
                result[1]=index-1;
				break;
			}
		}
        return result;
    }
}
C++实现:
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        //时间复杂度为O(logn)
		int n = nums.size();
		int l = 0, h = n-1, m;
		int start = -1, end = -1;
		while (l<=h)
        {
			m = l + ((h - l) >> 1);
			if (nums[m]>target)
            {
				h = m-1;
			}
			else if (nums[m]<target)
            {
				l = m + 1;
			}
			else
            {//找到以后,需要确定前后边界
				int index = m;
				while (index >= 0 && nums[index] == target)
                {
                    index--;
                }
				start = index + 1;
				index = m;
				while (index<n&&nums[index] == target)
                {
                    index++;
                }
				end = index - 1;
				break;
			}
		}
		return vector<int>{start,end};
    }
};
034 Search for a Range 搜索范围的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
		
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
 - 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 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 ...
 - Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
		
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
 - LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
		
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
 - [OJ] Search for a Range
		
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
 - [Leetcode][Python]34: Search for a Range
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
 - Leetcode::Longest Common Prefix && Search for a Range
		
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
 
随机推荐
- 洛谷【P1303】A*B Problem
			
题目传送门:https://www.luogu.org/problemnew/show/P1303 高精度乘法板子题,灵性地回忆一下小学时期列竖式的草稿纸即可. 时间复杂度:\(O(len^2)\) ...
 - BZOJ3680:吊打XXX
			
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
 - Apache Flume 1.6.0 发布,日志服务器
			
Apache Flume 1.6.0 发布,此版本现已提供下载: http://flume.apache.org/download.html 更新日志和文档: http://flume.apache. ...
 - windows服务和进程的区别和联系
			
Windows Service 是主要用于服务器环境而长期运行的应用程序, 这类程序不需要有用户界面或者任何模拟输出. 任何的用户消息通常都是记录在Windows 事件日志里.Windows Serv ...
 - uboot 命令使用教程(uboot参数设置)
			
1. Printenv 打印环境变量. uboot> printenv baudrate=115200 ipaddr=192.168.0.111 ethaddr=32:34:46:78:9A:D ...
 - oracle--分页过程demo1
			
oracle分页过程demo1: --ROWNUM用法 select o.*,rownum rn from (select * from emp) o where rownum<=10; sel ...
 - Fast Walsh–Hadamard transform
			
考虑变换 $$\hat{A_x} = \sum_{i\ or\ x = x}{ A_i }$$ 记 $S_{t}(A,x) = \sum_{c(i,t)\ or\ c(x,t)=c(x,t),\ i ...
 - OpenGL 使用GLFW创建全屏窗口
			
OpenGL 使用GLFW创建全屏窗口 GLFW库里面的glfwCreateWindow()函数是用来创建窗口的函数. 这样函数的原型是: GLFWwindow* glfwCreateWindow(i ...
 - fsck修复系统断电或非正常关机导致的系统磁盘问题
			
问题描述: unexpected inconsistency; run fask mannally. (i.e., without -a or -p options) fsck repaire man ...
 - UVa 11468 Substring (AC自动机+概率DP)
			
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...