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 ...
随机推荐
- 【HDU 5363】Key Set
题 Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if ...
- OI历程日常
之前的一直没来的及记录,表示从今往后连载 10.29 蒟蒻正在紧张的备战NOIP 整改了一下faebdc学长的模拟题,T1直接可以暴力破解,T2二分,O(nlog^2n)开始二分写残了,调了半天唉,现 ...
- POJ3264 Balanced Lineup
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 44720 Accepted: 20995 ...
- groovy-脚本和类
在groovy中定义类和java中是一样的.类的方法可以是static,也可以是非static的. groovy中的方法可以是public, protected, private,同时也支持java中 ...
- ASP.NET MVC 3 loginUrl自动变成Account/Login,并且发生404错误的解决方法
http://www.cnblogs.com/think8848/archive/2011/07/08/2100814.html ASP.NET MVC 3 loginUrl自动变成Account/L ...
- jquery------显示加载的js时出现中文乱码解决方法
方法: 把my.js文件复制出来,用记事本打开,再另存为的时候设置编码格式为utf-8即可
- list的使用命令 百度经验保存
在key对应的list的头部添加字符串元素 命令:lpush #参数0 到-1 是从开始到结束 2 在key对应list的尾部添加字符串元素: 命令:rpush 3 在k ...
- 点击cell弹出一个日期选择器
- (void)setUpGroup2 { ILGroupItem *group = [[ILGroupItem alloc] init]; // 结束时间 ILSettingItem *endTim ...
- sturct stat 结构体中 st_mode 的含义
工作中遇到 else if( (s_buf.st_mode&S_IFMT) == S_IFDIR) return 2; else if( !(s_buf.st_mode&S_IFREG ...
- dns (域名系统)
dns (域名系统) DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP ...