LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
if (nums[start] <= nums[mid]){//mid大于start
if (target < nums[mid] && target >= nums[start])// target在start和mid之间
end = mid - 1;
else
start = mid + 1;
}
if (nums[mid] <= nums[end]){//mid小于end
if (target > nums[mid] && target <= nums[end])//target在mid和end之间
start = mid + 1;
else
end = mid - 1;
}
package leetcode_50; /***
*
* @author pengfei_zheng
* 在数组中查找target 并返回其下标
*/
public class Solution33 {
public int search(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
while (start <= end){
int mid = (start + end) / 2;
if (nums[mid] == target)
return mid;
if (nums[start] <= nums[mid]){
if (target < nums[mid] && target >= nums[start])
end = mid - 1;
else
start = mid + 1;
}
if (nums[mid] <= nums[end]){
if (target > nums[mid] && target <= nums[end])
start = mid + 1;
else
end = mid - 1;
}
}
return -1;
}
}
LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)的更多相关文章
- 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 ...
- [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 [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 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 ...
随机推荐
- utf8_unicode_ci与utf8_general_ci的区别
From: http://www.chinaz.com/program/2010/0225/107151.shtml 当前,utf8_unicode_ci校对规则仅部分支持Unicode校对规则算法. ...
- phd文献阅读日志-博一下学期
博一下学期: 1.week1,2018.2.26 2006-Extreme learning machine: theory and applications 期刊来源:Huang G B, Zhu ...
- MATLAB出现:错误使用 xlsread (line 251)
背景: matlab 2015b和Excel2003 方法: 改Excel的Com加载项 步骤: 1 2 3 4
- Mongodb数据结构及与MySql对比
MySql一直是性价比最高的关系型数据库典范 MongoDB带来了关系数据库以外的NoSql体验. 让我们看一个简单的例子,我们将如何为MySQL(或任何关系数据库)和MongoDB中创建一个数据结构 ...
- 在ListView中使用GridView, Style这样写:
参考:http://msdn.microsoft.com/zh-cn/library/vstudio/ms788747.aspx 需求,自定义ListView中的滚动条 这岂不是很简单?刷刷刷写完了, ...
- AdoConnect-获取连接字符串 (工具)
使用ADO访问数据库时需要设置正确的连接字符串,为此特提供一个获取连接字符串的小工具,方便编程使用. 使用方法: 1.点击“连接字符串”,弹出数据链接属性对话框 2.可以使用“提供程序”新建数据源,也 ...
- 2018-10-29 A股主要指数的市盈率(PE)估值高度
全指材料(SH000987) - 2018-10-29日,当前值:11.9289,平均值:30.66,中位数:26.1407,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 全指 ...
- SQL Compare数据库比较工具 完全破解+使用教程
来源http://www.cnblogs.com/duci/articles/4482665.html 一.使用教程 SQL Compare是编程人员常用的比较两个数据库之间差异的工具.可以用来比较数 ...
- Java输出错误信息与调试信息
创建一个类,在该类的main()主方法中,使用System类中的out和err两个成员变量来完成调试与错误信息的输出. public class PrintErrorAndDebug { public ...
- Go之类型判断
boy := util.Boy{util.Person{"Eric", 19, "boy"}, "1"} var boyClone inte ...