[LeetCode] 33. 搜索旋转排序数组
2021-02-05 06:15
标签:class 排序 turn start div tar length int return 感觉这个题不难。。。做出来了 ------------------------------MT--------------------------------- 过几个月我一定会再试一次的! [LeetCode] 33. 搜索旋转排序数组 标签:class 排序 turn start div tar length int return 原文地址:https://www.cnblogs.com/doyi111/p/12791010.htmlclass Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
int mid;
while (start end) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
//前半部分有序,注意此处用小于等于
if (nums[start] nums[mid]) {
//target在前半部分
if (target >= nums[start] && target nums[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (target nums[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
}
下一篇:剑指Offer[Python版]
文章标题:[LeetCode] 33. 搜索旋转排序数组
文章链接:http://soscw.com/index.php/essay/51202.html