Java for LeetCode 033 Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
解题思路:
Tag中已经有提示用二分查找,关键是确定target在左半部分还是右半部分,建议画个图,能直观很多。
JAVA实现如下:
static public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
if (target == nums[(right + left) / 2])
return (right + left) / 2;
//右半部分为旋转区域
if (nums[(right + left) / 2] >= nums[left]) {
if (target >= nums[left] && target < nums[(right + left) / 2])
right = (right + left) / 2 - 1;
else
left = (right + left) / 2 + 1;
}
//左半部分为旋转区域
else {
if (target > nums[(right + left) / 2] && target <= nums[right])
left = (right + left) / 2 + 1;
else
right = (right + left) / 2 - 1;
}
}
return -1;
}
Java for LeetCode 033 Search in Rotated Sorted Array的更多相关文章
- 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] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- LeetCode 033 Search in Rotated Sorted Array
题目要求:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you b ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 【CodeForces 625A】Guest From the Past
题 题意 一升奶可以花费a元,也可以话b元买然后获得c元,一开始有n元,求最多买多少升奶. 分析 贪心,如果b-c<a,且n≥b,那就买b元的,n先减去b再看看够买多少瓶,然后再+1瓶,余款再购 ...
- 【poj3714】 Raid
http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...
- POJ3714 Raid
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10625 Accepted: 3192 Description ...
- Git Pull 避免用户名和密码方法
在开发中使用的版本控制器时git , 每次使用命令"git pull"从服务器获得最新代码时,都需要输入用户名和密码,这样浪费了大量的时间和热情,在此背景下,本文在网上找到解决版本 ...
- Knockout Grid - Loading Remote Data
http://wijmo.com/grid-with-knockout-viewmodel-loading-remote-data/ We were hearing quite a few peopl ...
- 初学structs2,简单配置
一.structs2-demo1项目下新建structs.xml文件,文件名必须是structs 二.package节点配置及其子节点配置 <!--name:单纯给包起个名字,保证和其他包不重名 ...
- tp框架做留言板
首先是登录的LoginController.class.php 代码内容是 <?php namespace Admin\Controller; use Think\Controller; cla ...
- web.xml文件中的web-app元素 部署
[转载]web.xml文件中的web-app元素 (2012-05-24 13:35:57) 转载▼ 标签: 转载 分类: java 挺全 的 呵呵呵 转了 原文地址:web.xml文件中的web-a ...
- mysql启动报错(mac)
$mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) ...
- poj1753枚举
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33670 Accepted: 14713 Descr ...