问题描述:给定一个有序序列,找到指定元素的起始和结束位置。例如:1234555,5,起始4结束6

算法分析:其实就是一个二分查找的利用。但是特殊就在不是找到某个元素,而是找到下标。也就是在nums[mid]=target时,要分析mid的左右元素。

public int[] searchRange(int[] nums, int target)
{
if(nums == null || nums.length == 0)
{
return null;
}
int[] arr = {-1,-1};
binarySearch(nums, 0, nums.length - 1, target, arr);
return arr;
} public void binarySearch(int[] nums, int left, int right, int target, int[] arr)
{
int mid = (left + right)/2;
if(left > right)
{
return;
}
if(nums[left] == target && nums[right] == target)//特例
{
arr[0] = left;
arr[1] = right;
return;
}
if(nums[mid] == target)
{
int templ = mid, tempr = mid;
while(templ>=left && nums[templ]==target)
{
templ --;
}
arr[0] = templ+1;
while(tempr<=right && nums[tempr]==target)
{
tempr ++;
}
arr[1] = tempr-1;
}
else if(nums[mid] < target)
{
binarySearch(nums, mid + 1, right, target, arr);
}
else
{
binarySearch(nums, left, mid - 1, target, arr);
}
}

Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置的更多相关文章

  1. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

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

  3. [LeetCode] Search for a Range 搜索一个范围

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

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

  6. leetcode:Search for a Range(数组,二分查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  9. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

随机推荐

  1. Less-mixin函数基础二

    //mixin函数 基础使用方法 --包含选择器,example: .test(){ &:hover{ border:1px solid red; } } button{ .test; } / ...

  2. Java利用dom4j生成xml文件、解析XML

    package com.fq.fanqi; import java.io.File;import java.io.FileWriter;import java.io.IOException;impor ...

  3. XXE(xml外部实体注入漏洞)

    实验内容 介绍XXE漏洞的触发方式和利用方法,简单介绍XXE漏洞的修复. 影响版本: libxml2.8.0版本 漏洞介绍 XXE Injection即XML External Entity Inje ...

  4. hdu 1513 Palindrome【LCS滚动数组】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. Powershell Get-FileHash

    File Hash (Get-FileHash C:\fso\myfile.txt).hash Get-FileHash C:\Users\Andris\Downloads\Contoso8_1_EN ...

  6. 如何看懂ORACLE执行计划

    如何看懂Oracle执行计划 一.什么是执行计划 An explain plan is a representation of the access path that is taken when a ...

  7. IE数组排序问题的处理

    有一哥们在微信开发中,到生成签名这抓狂了一天 最后发现微信调试工具在IE和chrome下对字符的排序竟然不同. 嗯,这个问题引起了我的关注,于是根据微信工具里的对象数组格式,撸了几句代码调试了一下,发 ...

  8. confirm() event.target.getAttribute('id')

    w <?php $wecho = '<form id="del' . $wid . '" method="POST" action="&q ...

  9. SpringMVC 运行流程以及与Spring 整合

    1. 运行流程 2. Spring 和 SpringMVC 整合 // 1. 导入 jar 包 // 2. 配置 web.xml <!-- 配置 Spring 的核心监听器 --> < ...

  10. IO 流之字节流和转换流

    基本读取操作: InputStream(); OutputStream(); // 直接写入目的地中, 不需要 flush() 刷新 write(byte[] b); // 参数为 byte 数组 字 ...