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. u3d 发布的程序 窗口位置的改变

    using System; using System.Runtime.InteropServices; using UnityEngine; public class WindowMOD : Mono ...

  2. java mysql 链接高版本出现SSL验证

    key1: String url="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 ...

  3. spark 非常好的学习内容

    http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html

  4. 【WP8】自定义配置存储类

    之前在WP7升级到WP8的时候遇到配置不兼容的问题 情景:之前只有一个WP7版本,现在需要发布WP8版本,让用户可以从原来的WP7版本升级到WP8版本 一般情况下从WP7升级到WP8没什么问题 但是在 ...

  5. java中substring的用法

    substring 1.public String substring(int beginIndex).     返回一个新的字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符,一直到 ...

  6. Windows 2008驱动安装失败的原因及解决方法

    希望这些内容能够帮助各位朋友顺利地在Windows Server 2008系统环境下安装使用好各种设备的驱动程序! 寻找安装失败原因 一般来说,当我们将目标设备的驱动安装光盘正确放置到Windows ...

  7. Java获取一维数组的最小值

    编写程序,实现接受用户在文本框中输入的单行数据.这些数据都是整数数字,以空格进行分隔,空格数量不限.并将这些数据分割成一维数组,再从数组中提取最小值显示在界面中.思路是先对用户的输入进行验证,即先用t ...

  8. 【NLP】pyhanlp flask

    D:\ProgramData\Anaconda3\Lib\site-packages\pyhanlp\__init__.py 加入 WordVectorModel = LazyLoadingJClas ...

  9. MFC之自绘控件

    在描绘MFC界面时,MFC自带的控件样式是绝对不满足界面的需求的. 所以我们就要在MFC自带控件基础上对控件样式进行重绘. 在采用自绘前界面样式 采用自绘后界面样式 是不是自绘控件后看起来正常了很多? ...

  10. RF失败案例重跑

    1.1        失败案例重跑 该功能主要是针对上次连跑失败的案例需要重新执行测试的情况,可自动识别上次执行失败的案例并进行重跑,无需手动选择相应的案例,简单高效. 1.5.1.        重 ...