您好,欢迎来到花图问答。
搜索
您的当前位置:首页这周一道算法题(六十二)

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

来源:花图问答

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

Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务