Search in Rotated Sorted Array
Problem
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 duplicate exists in the array. ExampleFor [4, 5, 1, 2, 3] and target=1, return 2. For [4, 5, 1, 2, 3] and target=0, return -1. ChallengeO(logN) time Note找中点:若起点小于中点,说明左半段没有旋转,否则说明右半段没有旋转。
Follow up for "Search in Rotated Sorted Array": Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Note只判断有无,就容易了。 Solution
|
小黑屋|在路上
( 蜀ICP备15035742号-1 )
GMT+8, 2025-5-6 15:51
Copyright 2015-2025 djqfx