给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置。
你的算法时间复杂度必须是 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 搜索范围的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. 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 ...

  3. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  8. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  9. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

随机推荐

  1. xml schema 中如何定义类似Map的结构

    利用xs:unique关键字.在xs:element里添加unique节点,任意命名,然后用xs:selector来选择需要唯一的域, xs:field 里指定特定的字段. 例如:定义所有Item里的 ...

  2. 添加一个用户并且让用户获得root权限

    1.创建一般用户: 完全参考默认值创建一个用户, 一般账号UID应该是500以后的. 默认会创建用户家目录和账号一模一样的群组名.创建使用账号且给予口令才算完成了用户的创建流程. useradd us ...

  3. UITextField常见用法

    //实例变量和全局变量的区别 //1.定义位置有区别:全局变量定义在方法的外部,实例变量写在接口文件或者延展中的大括号之内 //2.生命周期:全局变量生命周期和应用程序生命周期相同,实例变量的生命周期 ...

  4. 机器学习:从sklearn中加载数据

    一.sklearn模块 sklearn模块下有很多子模块,常用的数据集在:sklearn.datasets模块下: 通过数据集中DESCR来查看数据集的文档: 从datasets中加载数据: impo ...

  5. 【转】 Pro Android学习笔记(五八):Preferences(2):CheckBoxPreference

    目录(?)[-] CheckBox Preference xml文件 设备的存贮文件 复合preference 在ListPreference的例子中显示的是单选,如果是多选,可采用CheckBoxP ...

  6. Linux负载均衡软件之LVS

    一. LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,它的官方站点是linuxvirtualserver.org ...

  7. 微服务学习一 微服务session 管理

    集群和分布式架构中: session管理有三种方法: 1: Cookie: 将Session对象保存在Cookie,保存在浏览器端.浏览器发送请求的时候,会把整个session放在请求里一起发送到se ...

  8. 使用python对文件夹里面所有代码行数进行统计。

    统计目录下所有的代码个数和总行数. # -*- coding: utf-8 -*- # @Author : ydf import json import os from pathlib import ...

  9. shell批量创建用户随机密码

    批量创建用户随机密码企业面试题3:批量创建10个系统帐号usr01-usr10并设置密码(密码为随机8位字符串). #! /bin/bash . /etc/init.d/functions Path= ...

  10. 网络编程之socket编程

    TCP/IP协议 网络层的“ip地址”可以唯一标识网络中的主机,而传输层的“协议+端口”可以唯一标识主机中的应用程序(进程).这样利用三元组(ip地址,协议,端口)就可以标识网络的进程了,网络中的进程 ...