Cover image for Interview Classic 150 Questions P33 Search in Rotated Sorted Array

Interview Classic 150 Questions P33 Search in Rotated Sorted Array


Timeline

Timeline

2025-12-04

init

Divide and Conquer

Problem:

The essence of binary search is to rule out the other half:

  • ifleft halfis increasing and target is not in this interval, then target must be in theright half
  • ifright halfis increasing and target is not in this interval, then target must be in theleft half
123456789101112131415161718192021222324252627282930313233343536
#include <vector>using std::vector;class Solution {    public:        int search(vector<int> &nums, int target)        {                int n = nums.size();                int left = 0, right = n - 1;                int mid;                while (left <= right) {                        mid = left + (right - left) / 2;                        if (nums[mid] == target) {                                return mid;                        }                        if (nums[left] <= nums[mid]) {                                if (nums[left] <= target &&                                    target < nums[mid]) {                                        right = mid - 1;                                } else {                                        left = mid + 1;                                }                        }else if (nums[right] >= nums[mid]) {                                if (nums[mid] < target &&                                    target <= nums[right]) {                                        left = mid + 1;                                } else {                                        right = mid - 1;                                }                        }                }                return -1;        }};

leetcode hot 100 rewrite

1234567891011121314151617181920212223242526272829303132333435363738394041
#include <vector>using std::vector;class Solution {    public:        int search(vector<int> &nums, int target)        {                // 1 <= nums.length <= 5000                // -104 <= nums[i] <= 104                // Every value in nums is unique.                // The problem data guarantees that nums was rotated at some previously unknown index.                // -104 <= target <= 104                int n = nums.size();                int left = 0, right = n - 1, mid;                while (left <= right) {                        mid = left + (right - left) / 2;                        if (target == nums[mid])                                return mid;                        if (nums[mid] < nums[right]) {                                if (target > nums[mid] && target <= nums[right]) // on the left side of the ascending segment                                        left = mid + 1;                                else                                        right = mid - 1;                        } else { // in the reversed segment                                if (target < nums[mid] && target >= nums[left]) // on the left side of the reversed segment                                        right = mid - 1;                                else                                        left = mid + 1;                        }                }                return -1;        }};
Loading comments…