033. Search in Rotated Sorted Array

 1 class Solution {
 2 public:
 3     int search(vector<int>& nums, int target) {
 4         if (nums.size() == 0) return -1;
 5         else {
 6             int left = 0, right = nums.size() - 1;
 7             while (left <= right) {
 8                 int mid = left + (right - left) / 2;
 9                 //cout << "left = " << left << ‘\t‘ << "right = " << right << endl;
10                 if (nums[mid] == target) return mid;
11                 else if (nums[mid] < target) {
12                     if (nums[mid] < nums[right]) {
13                         if (nums[right] == target) return right;
14                         else if (nums[right] < target) right = mid - 1;
15                         else left = mid + 1;
16                     }
17                     else {
18                         left = mid + 1;
19                     }
20                 }
21                 else {
22                     if (nums[mid] < nums[right]) right = mid - 1;
23                     else {
24                         if (nums[left] == target) return left;
25                         else if (nums[left] < target) right = mid - 1;
26                         else  left = mid + 1;
27                     }
28                 }
29             }
30             return -1;
31         }
32     }
33 };
时间: 2024-12-18 23:53:09

033. Search in Rotated Sorted Array的相关文章

[LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. Search in Rotated Sorted Array (Hard) 链接: 题目:https://leetcode.com/problems/search-in-rotated-sorted-array/ 代码(github):https://github.com/illuz/leetcode 题

LeetCode 033 Search in Rotated Sorted Array

题目要求: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

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 duplic

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 affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

LeetCode: Search in Rotated Sorted Array

LeetCode: 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, othe

[LeetCode] Search in Rotated Sorted Array I (33) &amp;&amp; II (81) 解题思路

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

【leetcode】Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 与I类似,只是在二分搜

【leetcode】Search in Rotated Sorted Array

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 retu