Java for LeetCode 034 Search for a Range
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].
解题思路:
看到O(log n) 几乎可以肯定是二分查找的思路,题目不是特别难的那种,仔细想想就想出来了,JAVA实现如下:
static public int[] searchRange(int[] nums, int target) {
int[] result = new int[2];
result[0] = result[1] = -1;
int left = 0, right = nums.length - 1;
while (left <= right) {
if (nums[(left + right) / 2] > target)
right = (left + right) / 2 - 1;
else if (nums[(left + right) / 2] < target)
left = (left + right) / 2 + 1;
else {
result[0] = result[1] = (left + right) / 2;
while (target != nums[left]) {
if (target > nums[(result[0] + left) / 2])
left = (result[0] + left) / 2 + 1;
else {
result[0] = (result[0] + left) / 2;
left++;
}
}
result[0] = left;
while (target != nums[right]) {
if (target < nums[(result[1] + right) / 2])
right = (result[1] + right) / 2 - 1;
else {
result[1] = (result[1] + right) / 2;
right--;
}
}
result[1] = right;
break;
}
}
return result;
}
Java for LeetCode 034 Search for a Range的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of 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 ...
- Java for LeetCode 081 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [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 ...
- 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 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- The big deffrence between ($(du * )) and $(du *)
Infolist=($(du *))echo "Get the list one $Infolist"This has formed a array after quating t ...
- 【前端学习】sublime开启vim模式
学习目标:在sublime下开启vim模式,了解基本vim的编辑快捷键. 下载安装Sublime Text 3 :http://www.sublimetext.com/3 Vim/Vi: Vim/Vi ...
- BZOJ4241 历史研究
Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开始着手调查日记中记载的事件. ...
- java将数据写入到txt文件中(txt有固定的格式)
java将数据写入到txt文件中,这个应该对于学过java I/O的人来说是很简单的事情了,但是如果要将数据以固定的格式写入到txt文件中,就需要一定的技巧了. 这里举个简单的例子,以供参考: 比如我 ...
- sleep()
经常看到线程中用sleep(),到底是什么用处,下面讲的比较通俗: 我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: ...
- 第一部分 mongodb 基础篇
什么是NoSQL认识MongoDBMongDB的下载与安装MongoDB的体系结构常用命令(基本的增删改查)客户端GUI工具集合 一: 什么是NoSql1 NoSQL简介NoSQL是Not Only ...
- mysql 外键(FOREIGN KEY)
最近有开始做一个实验室管理系统,因为分了几个表进行存储·所以要维护表间的关联··研究了一下MySQL的外键. (1)只有InnoDB类型的表才可以使用外键,mysql默认是MyISAM,这种类型不支持 ...
- [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表
写在前面 本篇文章将实现应用列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘 ...
- RedHat linux配置yum本地资源
今天安装个linux平台的oracle数据库,在oracle检测的时候发现有些系统补丁包没有安装,手动rpm安装报有些关联包也没有安装,想想如果一个一个rpm的话累死人了要,所以想起用yum来进行安装 ...
- motto7
与其羡慕别人,还不如模仿别人的过程.