Search in Rotated Sorted Array ( LeetCode ) Problem Solution.
You are given a target value to look. If found within the array return its index, otherwise return
-1
.
You may assume no duplicate exists within the array.
Your algorithm's runtime complexity within the order of O(log n).
Example 1:
Input: nums = [4,5,6,7,0,1,2]
, target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2]
, target = 3
Output: -1
Solution:-
class Solution:
def search(self, nums: List[int], target: int) -> int:
ans=0
if(len(nums)==0):
return -1
else:
for i in range(0,len(nums)):
if nums[i]== target:
ans=i
break
else:
ans=-1
return ans
Runtime: 36 ms, faster than 87.91% of Python3 online submissions for Search in Rotated Sorted Array.Memory Usage: 14.2 MB, but 6.29% of Python3 online submissions for Search in Rotated Sorted Array.
0 Comments