美文网首页
这周一道算法题(六十二)

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

作者: CrazySteven | 来源:发表于2018-08-05 21:47 被阅读30次

本周题目难度 级别'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 原创出品,欢迎转载,转载时请注明出处!

相关文章

网友评论

      本文标题:这周一道算法题(六十二)

      本文链接:https://www.haomeiwen.com/subject/brjivftx.html