zoukankan      html  css  js  c++  java
  • 剑指 Offer 61. 扑克牌中的顺子

    • 题目描述
    从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。
    
     
    
    示例 1:
    
    输入: [1,2,3,4,5]
    输出: True
     
    
    示例 2:
    
    输入: [0,0,1,2,5]
    输出: True
    • 题目解析

    这道题没有什么特别的考点,我觉得只要需要抓住两个点:

    1.要组成顺子,那么list的最大值和最小值的差值一定要小于5

    2.组成顺子,list不能有除0外重复的数。

    class Solution:
        def isStraight(self, nums: List[int]) -> bool:
            s = set()
            max_num = 1
            min_num = 14
            for n in nums:
                if n == 0:
                    continue
                else:
                    if n in s:
                        return False
                    else:
                        max_num = max(max_num, n)
                        min_num = min(min_num, n)
                        s.add(n)
            return max_num - min_num < 5
  • 相关阅读:
    VBA开发手记
    爬虫之Scrapy框架
    RPA 介绍
    MongoDB入门
    爬虫项目汇总
    coding基本功实践
    wxpy使用
    爬虫-工具篇
    SQLAlchemy使用介绍
    wtforms组件使用实例及源码解析
  • 原文地址:https://www.cnblogs.com/yeshengCqupt/p/13591621.html
Copyright © 2011-2022 走看看