Java [leetcode 33]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.
解题思路:
采用二分查找法查找,若最后没有找到返回-1,否则过程中返回具体的位置。
代码如下:
public class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
if (nums[left] == target)
return left;
if (nums[right] == target)
return right;
if (nums[mid] > nums[left]) {
if (target < nums[mid] && target > nums[left])
right = mid - 1;
else
left = mid + 1;
} else {
if (target > nums[mid] && target < nums[right])
left = mid + 1;
else
right = mid - 1;
}
}
return -1;
}
}
Java [leetcode 33]Search in Rotated Sorted Array的更多相关文章
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [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. ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode 33.Search in Rotated Sorted Array(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- leetcode 33. 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 migh ...
- [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. ...
- leetcode 33 Search in Rotated Sorted Array JAVA
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个 ...
随机推荐
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- 微软职位内部推荐-Principal Architect for Windows Security
微软近期Open的职位: Location: China, BeijingDivision: Operations System Group Engineering Group OverviewOSG ...
- sql shard/partition
sql http://www.infoq.com/news/2011/02/SQL-Sharding/ http://channel9.msdn.com/Shows/Data-Exposed/SqlD ...
- Homework3
1. 切换路径到想要保存的目录下,使用git init创建仓库: 2. 在仓库下创建文件Readme.txt,使用git add命令添加到暂存区和 git commit 命令提交到仓库: 3. 修改 ...
- 机器学习基石的泛化理论及VC维部分整理(第六讲)
第六讲 第五讲主要讲了机器学习可能性,两个问题,(1)\(E_{in} 要和 E_{out}\) 有很接近,(2)\(E_{in}\)要足够小. 对于第一个假设,根据Hoefding's Inequa ...
- poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
- mysql分表与分区表
mysql分表与分区表 转自:http://blog.51yip.com/mysql/949.html 一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具 ...
- python读取mnist
python读取mnist 其实就是python怎么读取binnary file mnist的结构如下,选取train-images TRAINING SET IMAGE FILE (train-im ...
- XP纯净版光盘ISO镜像文件
原版xp的下载地址,直接发链接得了-.-打开迅雷,按新建任务,把下面下载地址复制进来,下载就可以了 thunder://QUFodHRwOi8vc29mdC51c2Fpa2EuY24vstnX98+1 ...
- http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html
http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html