Leetcode 81. Search in Rotated Sorted Array II

Problem

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Algorithm

Just use O(n) algorithm.

Code

class Solution:
    def search(self, nums: List[int], target: int) -> bool:
        if not nums:
            return False
        
        for num in nums:
            if num == target:
                return True
        return False

你可能感兴趣的:(解题报告)