【数学专题】剑指 Offer 61. 扑克牌中的顺子

本问题对应课程链接:剑指offer刷题第六期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 15 个回答

  • kfaebot 普通 2024年3月27日 下午5:07
    class Solution {
    public:
        bool checkDynasty(vector<int>& places) {
            //创建哈希表
            unordered_set<int>id;
            //因为每排文物为5个,若最大和最小编号之差小于5,说明是连续的
            //最大id
            int maxid=-1;
            //最小id
            int minid=14;
    
            for(int i=0;i<places.size();i++){
                if(places[i]==0){
                    continue;
                }
                maxid=max(maxid,places[i]);
                minid=min(minid,places[i]);
                if(id.find(places[i])==id.end()){
                    id.insert(places[i]);
                }
                else{
                    return false;
                }
            }
            if(maxid-minid<5){
                return true;
            }
            else{
                return false;
            }
        }
    };
    
    0 赞同 0 条回复
  • 一个达不刘 普通 2024年3月25日 上午10:43

    class Solution {
    public:
    bool checkDynasty(vector& places) {
    //max-min < 5,即满足条件。因为总共5张牌,13个号码。若是
    int unknown = 0;
    sort(places.begin(), places.end()); // 数组排序
    for(int i = 0; i < 4; i++) {
    if(places[i] 0) unknown++; // 统计未知朝代数量
    else if(places[i]
    places[i + 1]) return false; // 若有重复,提前返回 false
    }
    return places[4] – places[unknown] < 5; // 最大编号朝代 – 最小编号朝代 < 5 则连续
    }

    };

    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月24日 下午10:53

    class Solution {
    public:
    bool checkDynasty(vector& places) {
    set
    s;
    int min_num = 14;
    int max_num = -1;
    for(int i = 0; i < places.size(); i++)
    {
    if(places[i] 0)
    continue;
    if(s.count(places[i]))
    return false;
    s.insert(places[i]);
    min_num = min(min_num, places[i]);
    max_num = max(max_num, places[i]);
    }
    return max_num – min_num < 5;
    }
    };
    时间和空间复杂度都是O(n)

    0 赞同 0 条回复
  • 言之命至 普通 2024年3月24日 下午9:08

    如果要成为一个顺子,则需要 满足两个条件 (充要条件)
    1、不存在有重复的数,大小王除外
    2、最大值 – 最小值 < 5,大小王除外

    class Solution {
        public boolean isStraight(int[] nums) {
    
            Set<Integer> set = new HashSet<>();
            int max = -1, min = 20;
            for(int i = 0; i < nums.length; i++){
                if(nums[i] == 0) continue; //大小王
                if(set.contains(nums[i]))return false;//判断是否有重复
                set.add(nums[i]);//加进来
                max = Math.max(nums[i], max);
                min = Math.min(nums[i], min); //统计set里面的最大值和最小值
            }
    
            return max - min < 5;
        }
    }
    

    时间复杂度:O(n)

    空间复杂度:O(n)

    0 赞同 0 条回复
  • Jimmy 普通 2024年3月24日 下午1:48

    // 集合 n,n// 如果要成为一个顺子,则需要 满足两个条件
    // 1、不存在有重复的数,大小王除外
    // 2、最大值 – 最小值 < 5,大小王除外

    class Solution {
    public:
        bool checkDynasty(vector<int>& places) {
            set<int> s;
            int max_num = -1,min_num = 20;
            for(int i=0;i<places.size();i++){
                if(places[i] == 0)continue;
                if(s.count(places[i]))return false;//有重复元素就肯定不连续
                s.insert(places[i]);
                max_num = max(max_num,places[i]);
                min_num = min(min_num,places[i]);
    
            }
            return max_num-min_num < 5;
        }
    };
    
    0 赞同 0 条回复
  • 白雪公主倒拔垂杨柳 永久会员 2024年3月24日 上午11:37
    class Solution {
        public boolean checkDynasty(int[] places) {
            Set<Integer> set = new HashSet<>();
            int max=-1;
            int min=100;
    
            for(int i=0; i < places.length;i++){
                if(places[i]==0) continue;
                if(set.contains(places[i])) return false;
    
                set.add(places[i]);
                max=Math.max(max,places[i]);
                min=Math.min(min,places[i]);
            }
    
            return max-min<5;
        }
    }
    

    时间复杂度:O(n)
    额外空间复杂度:O(n)

    0 赞同 0 条回复
  • 机电皮怪 永久会员 2024年3月23日 下午11:19

    先排序再判断
    时间复杂度O(5)
    空间复杂度O(1)

    “`C++
    class Solution {
    public:
    bool checkDynasty(vector& places) {
    int unknown = 0;
    sort(places.begin(), places.end());
    for(int i = 0; i < places.size() - 1; i++) { if(places[i] == 0) { unknown++; continue; } else if(places[i] == places[i + 1]) { return false; } } return places[4] - places[unknown] < 5; } }; ```

    0 赞同 0 条回复
  • 云无深雁影 普通 2024年3月23日 下午10:46

    class Solution {
    public boolean checkDynasty(int[] places) {
    Set set= new HashSet<>();
    int min = 14;
    int max = -1;
    for(int place : places){
    if(place 0)
    continue;
    else if(set.contains(place))
    return false;
    else{
    set.add(place);
    min = Math.min(min,place);
    max = Math.max(max,place);
    }
    }
    System.out.println(max – min);
    return max – min < 5;

    }
    

    }
    时间O1
    空间O1

    0 赞同 0 条回复
  • Thaddeus 永久会员 2024年3月23日 下午8:43

    打卡

    class Solution {
    public:
        bool checkDynasty(vector<int>& places) {
            unordered_set<int> numSet;
            int _max = 0, _min = 14;
            for(int i = 0; i < places.size(); ++i) {
                if(places[i] == 0) continue;
                if(numSet.count(places[i])) return false;
                numSet.insert(places[i]);
                _max = max(_max, places[i]);
                _min = min(_min, places[i]);
            }
            return (_max - _min) < 5;
        }
    };
    
    0 赞同 0 条回复
  • 方铎码农 普通 2024年3月23日 下午7:25
    class Solution {
        public boolean checkDynasty(int[] places) {
            // 辅助哈希表
            Set<Integer> repeat = new HashSet<>();
            int max = 0, min = 14;
            for(int place:places){
                if(place == 0) continue;
                max = Math.max(max,place);
                min = Math.min(min,place);
                if(repeat.contains(place)) return false; //来自 13 个朝代的文物,朝代不重复
                repeat.add(place);
            }
            return max-min<5;   //五个文物,如果数字差大过5,一定不连续
        }
    }
    

    时间和空间复杂度都是$O(1)$

    0 赞同 0 条回复
  • Yox 普通 2024年3月23日 上午2:46

    class Solution:
    def checkDynasty(self, places: List[int]) -> bool:
    repeat = set()
    mi = 14
    ma = 0
    for place in places:
    if place 0: continue
    mi = min(mi,place)
    ma = max(ma,place)
    if place in repeat:return False
    else: repeat.add(place)
    return ma- mi <5
    时间0 1 空间 01

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月22日 下午10:24

    java
    class Solution {
    public boolean checkDynasty(int[] places) {
    Set repeat = new HashSet<>();
    int max = 0, min = 14;
    for(int place : places){
    if(place 0) continue;
    max = Math.max(max, place);
    min = Math.min(min, place);
    if(repeat.contains(place)) return false;
    repeat.add(place);
    }
    return max – min < 5;
    }
    }
    提交

    0 赞同 0 条回复
  • 紫芝 普通 2024年3月22日 下午9:40

    思路:给定数组中的数字需要满足两个条件:
    1. 不包含重复数字,用set查询
    2. 除了0之外,最大值与最小值的差值小于5,则可以组成顺子,否则不能。

    “`c++
    class Solution {
    public:
    bool checkDynasty(vector& places) {
    set
    s;
    int mini = 14, maxi = -1, count = 0;
    for (int i=0; i

    0 赞同 0 条回复
  • 王位詮釋 普通 2024年3月22日 下午9:26
    刷地的最优解
    class Solution {
        public boolean checkDynasty(int[] places) {
            Set<Integer> set = new HashSet<>();
            int max = -1;
            int min = 20;
            for(int i=0;i<places.length;i++){
                if(places[i]==0){
                    continue;
                }
                if(set.contains(places[i])){
                    return false;
                }else{
                    set.add(places[i]);
                    max = Math.max(places[i],max);
                    min = Math.min(places[i],min);
                }
            }
            return max-min<5;
        }
    }
    
    0 赞同 0 条回复
  • heathhou 普通 2024年3月22日 下午9:22
    class Solution {
    public:
        bool checkDynasty(vector<int>& places) {
            set<int> s;
            int max_num = -1;
            int min_num = 20;
            for(int i = 0; i < places.size(); ++i){
                if(places[i] == 0){
                    continue;
                }
                if(s.count(places[i])){
                    return false;
                }
                s.insert(places[i]);
                max_num = max(max_num, places[i]);
                min_num = min(min_num, places[i]);
            }
    
            return max_num - min_num < 5;
        }
    };
    

    时间复杂度:O(n)
    空间复杂度:O(n)

    0 赞同 0 条回复

# 回答此问题

后才能回答