整数数组 nums
按升序排列,数组中的值 互不相同 。
在传递给函数之前,nums
在预先未知的某个下标 k
(0 <= k < nums.length
)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
(下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7]
在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2]
。
给你 旋转后 的数组 nums
和一个整数 target
,如果 nums
中存在这个目标值 target
,则返回它的下标,否则返回 -1
。
你必须设计一个时间复杂度为 O(log n)
的算法解决此问题。
输入:
nums = [4,5,6,7,0,1,2], target = 0
输出:
4
输入:
nums = [4,5,6,7,0,1,2], target = 3
输出:
-1
输入:
nums = [1], target = 0
输出:
-1
impl Solution {
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
let n = nums.len();
if n == 0 {
return -1;
}
if n == 1 {
return if nums[0] == target {
0
} else {
-1
}
}
let (mut l, mut r) = (0, n - 1);
while l <= r {
let mid = (l + r) >> 1;
if nums[mid] == target {
return mid as i32;
}
if nums[0] <= nums[mid] {
if nums[0] <= target && target < nums[mid] {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if nums[mid] < target && target <= nums[n - 1] {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
func search(nums []int, target int) int {
n := len(nums)
if n == 0 {
return -1
}
if n == 1 {
if nums[0] == target {
return 0
} else {
return -1
}
}
l, r := 0, n-1
for l <= r {
mid := (l + r) >> 1
if nums[mid] == target {
return mid
}
if nums[0] <= nums[mid] {
if nums[0] <= target && target < nums[mid] {
r = mid - 1
} else {
l = mid + 1
}
} else {
if nums[mid] < target && target <= nums[n-1] {
l = mid + 1
} else {
r = mid - 1
}
}
}
return -1
}
class Solution {
public:
int search(vector<int>& nums, int target) {
const int n = nums.size();
if (!n) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) { return mid; }
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
};
int search(int* nums, int numsSize, int target){
if (!numsSize) {
return -1;
}
if (numsSize == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = numsSize - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) { return mid; }
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[numsSize - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
class Solution:
def search(self, nums: List[int], target: int) -> int:
if not nums:
return -1
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) >> 1
if nums[mid] == target:
return mid
if nums[0] <= nums[mid]:
if nums[0] <= target < nums[mid]:
r = mid - 1
else:
l = mid + 1
else:
if nums[mid] < target <= nums[len(nums) - 1]:
l = mid + 1
else:
r = mid - 1
return -1
class Solution {
public int search(int[] nums, int target) {
final int n = nums.length;
if (n == 0) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) {
return mid;
}
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~