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. 通过 Service 访问 Pod

    我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 controller 会通过动态创建和销毁 Pod 来保 ...

  2. ubuntu下gedit和vim输入中文和中文显示

    安装和配置VIM,参考   http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...

  3. 基于ThinkPHP的在线编辑器调用

    开源的在线编辑器有很多,比如FCKEditor,UEditor,Kindeditor等,调用方式也都大同小异 下面列举UEditor在线编辑器插件在ThinkPHP里面的应用 1.Ueditor下载地 ...

  4. font-awesome 使用方法

    需要引入文件 font-awesome.css <link rel="stylesheet" href="{$yf_theme_path}public/font-a ...

  5. docker 相关文章

    https://baijiahao.baidu.com/s?id=1581420975184566963&wfr=spider&for=pc    创建centos基础镜像 https ...

  6. Linux命令缩写的全称

    [目录|文件] ls : list(列出目录内容) pwd : print work directory(打印当前目录,现示当前工作目录的绝对路径) cd : change directory(改变目 ...

  7. 开源一个简易轻量的reactor网络框架

    github https://github.com/sea-boat/net-reactor net-reactor it's a simple and easy net framework with ...

  8. 【Windows socket+IP+UDP+TCP】网络基础

    Windows Socket+网络      Winsock是 Windows下套接字标准.          Winsock 编程分为UDP[Windows socket + UDP],TCP[Wi ...

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

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

  10. Windows 7 无密码文件共享

    Windows7中创建无密码的文件共享的几个步骤: 在“控制面板\所有控制面板项\网络和共享中心\高级共享设置”开启“关闭密码保护共享”和“启用文件和打印机共享”.关闭密码保护共享的操作会启用Gues ...