Problem: 在已知递减排序的数组中,查找到给定数字的起止下标
 
采用两遍扫描;
第一遍扫描得到给定数字的起始下标,(从下标i==0开始到nums.lenght-1)
第二遍扫描从第一遍扫描得到的下标开始进行扫描 
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 数组中找到target起止下标
*/
public class Solution34 {
public static int[] searchRange(int[] nums, int target) { int start = 0, end = nums.length-1; int []ans = {-1,-1}; while(start<=end){
int mid = (start + end)/2;
if(nums[mid] >= target)
end = mid - 1;
else
start = mid + 1;
if(nums[mid]==target){
ans[0]=mid;
}
}
start = ans[0]==-1 ? 0 : ans[0];
end=nums.length-1;
while(start<=end){
int mid = (start+end)/2;
if(nums[mid]<=target)
start = mid + 1;
else
end = mid - 1;
if(nums[mid]==target)
ans[1]=mid;
}
return ans;
}
public static void main(String[]args){
int []nums = {5, 7, 7, 8, 8, 10};
int []ans = {0};
ans = searchRange(nums,8);
for(int item:ans){
System.out.print(item+" ");
}
}
}

LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. [LeetCode每日一题]80. 删除有序数组中的重复项 II

    [LeetCode每日一题]80. 删除有序数组中的重复项 II 问题 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使用额外 ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)

    ##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...

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

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

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

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

随机推荐

  1. Linux 系统目录介绍

    bin : bin 是Binary 二进制的缩写,就是可执行文件了.Bin目录下是用户常用的命令. sbin: 此目录下也是二进制文件 ,不过这里的命令是 超级用户如 root 这样的用户使用的. e ...

  2. 解决ESXi有虚拟机模版部署的CentOS虚拟机,网卡eth0找不到问题

    1,问题和虚拟机克隆后出现网卡找不到问题类似. 2,修改主机名hostname 修改 /etc/sysconfig/network文件 3,删除/etc/sysconfig/network-scrip ...

  3. tftp + bras

    Ubuntu 12.04 网卡设置 开发板ip时192.168.0.2,设置的服务ip是192.168.0.1 因此在主机上/etc/network/interfaces添加如下内容 auto eth ...

  4. 关系型数据库 和 非关系型数据对比 以及 MySQL与Oracle对比

    一.关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库.    关系模型1970年提出的,关系模型的概念得到了充分的发展并逐渐成为主流数据库结构的主流模型.    简单来说,关系模型指的 ...

  5. ubuntu14下搭建svn

    1.安装 查看是否安装 svn help 安装了卸载 sudo apt-get remove --purge subversion 安装 sudo apt-get update sudo apt-ge ...

  6. 使用jstl+el表达式遇到的几个问题

    1.使用jstl访问Map<Integer,String>中的内容时总取不到? el表达式的一个bug,在解析数字的时候,会自动将数字转换成Long类型. 我的解决办法是,Map的key改 ...

  7. 破解IT运维成本困境,专业化分工是妙方

    随着IT建设的不断深入和发展,IT运维成为了企业运营的必需品.许多企业的IT预算相比于去年虽然有了很大的提高,但总体来说还是非常紧张.上周,我参加了一个CIO沙龙研讨会,现场调查问到目前CIO在IT运 ...

  8. CentOS6.8手动安装MySQL5.6

    众所周知,mysql5.7推出后有很多没有填好的坑,对于老的系统和项目兼容性也存在问题,所以现在普遍的web项目还是应该跑在centos6.8+mysql5.6的环境之下,今天主要说一下mysql5. ...

  9. WIN7隐藏GUEST登录账户

    在Windows7中,我们有时候需要开启Guest用户,以方便给别的同事共享打印机和部分文件,但同时又不希望别人用Guest账号从本地登陆界面进入本机.这个时候就需要将本地登陆界面的Guest用户进行 ...

  10. Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis

    要利用Mybatis首先是需要导入mybatis-x.x.x.jar,其次,要整合Spring和Mybatis需要导入mybatis-spring-x.x.x.jar. JAR : mybatis-x ...