Search for a Range 解答
Question
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
Solution
Use binary search, first, find left position, then, find right position.
 public class Solution {
     public int[] searchRange(int[] nums, int target) {
         int[] result = new int[2];
         result[0] = -1;
         result[1] = -1;
         if (nums == null || nums.length < 1)
             return result;
         int start = 0, end = nums.length - 1, mid, first, last;
         // Find first position of target
         while (start + 1 < end) {
             mid = (end - start) / 2 + start;
             if (nums[mid] >= target)
                 end = mid;
             else
                 start = mid;
         }
         if (nums[start] == target)
             result[0] = start;
         else if (nums[end] == target)
             result[0] = end;
         // Find last position of target
         start = 0;
         end = nums.length - 1;
         while (start + 1 < end) {
             mid = (end - start) / 2 + start;
             if (nums[mid] <= target)
                 start = mid;
             else
                 end = mid;
         }
         if (nums[end] == target)
             result[1] = end;
         else if (nums[start] == target)
             result[1] = start;
         return result;
     }
 }
Search for a Range 解答的更多相关文章
- 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 ...
 - 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 ...
 - [OJ] Search for a Range
		
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
 - [LeetCode] 034. Search for a Range (Medium) (C++/Java)
		
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
 - [Leetcode][Python]34: Search for a Range
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
 - 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 ...
 - Leetcode::Longest Common Prefix && Search for a Range
		
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
 - [array]  leetcode - 34. Search for a Range - Medium
		
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
 - 【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 ...
 
随机推荐
- Python安装MySQLdb并连接MySQL数据库
			
当然了,前提是你已经安装了Python和MySQL.我的Python是2.6版本的. Python2.6的“Set”有点兼容性问题,自己照着改一下: http://sourceforge.net/fo ...
 - NetAnalyzer笔记 之 八 NetAnalyzer2016使用方法(2)
			
[创建时间:2016-05-06 22:07:00] NetAnalyzer下载地址 在写本篇的时候,NetAnalyzer 3.1版本已经发布,所以本篇就以最新版本的为例继续使用,并且顺带说明一下, ...
 - javascript实现的有缩略图功能的幻灯片切换效果
			
不久前写了一个简单的图片效果,没想到那么快就要用到项目中,所以功能方面要丰富一下: 主要改进: 1# 用圆点代替之前简单的页数显示,并且点击圆点可以显示对应图片: 2# 点击圆点,显示对应图片的缩略图 ...
 - XMLHTTP请求的当前状态
			
readyState,此属性只读,状态用长度为4的整型表示.定义如下: 0 (未初始化) 对象已建立,但是尚未初始化(尚未调用open方法) 1 (初始化) 对象已建立,尚未调用send方法 2 (发 ...
 - oracle(天猫处方药留言sql)
			
" ?> .dtd" > <sqlMap namespace="TmallTcMessage"> <typeAlias alias ...
 - Action Result
			
操作返回的内容成为操作结果 大多数情况下返回ViewResult,基类ActionResult 6钟标准类型: ViewResult:视图结果,包含HTML标记等元素 EmptyResult:空结果 ...
 - 使用Dataset
			
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
 - 安卓状态栏通知Status Bar Notification
			
安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...
 - EMCA常用命令 【weber整理必出精品】
			
EMCA常用命令 创建一个EM资料库 emca -repos create 重建一个EM资料库 emca -repos recreate 删除一个EM资料库 emca -repos drop 配置数据 ...
 - html5新特性--音频视频,拖放
			
1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...