这周一道算法题(六十二)

2018-08-05  本文已影响30人  CrazySteven

本周题目难度 级别'Medium',使用语言'Python'

题目:给你一个target值和数组(从小到大排序后在随机的一点上进行旋转如 [0,0,1,2,2,5,6] 从第二个2处开始旋转,则变为[2,5,6,0,0,1,2]),判断target是否在数组中。eg:[2,5,6,0,0,1,2],target:3。返回False

思路:最笨的遍历一遍,看看nums中有没有target就行了,不写注释了:

class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: bool
        """
        for i in nums:
            if i == target:
                return True
        return False

本以为会超时,结果一次就过了,只是效率略低,然后再优化下:

class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: bool
        """
        return target in nums

就酱~

版权声明:本文为 Crazy Steven 原创出品,欢迎转载,转载时请注明出处!

上一篇 下一篇

猜你喜欢

热点阅读