剑指offer刷题笔记--Num61-68

1--扑克牌中的顺子(61)

主要思路:

        五个数是顺子的充要条件:① 最大值 - 最小值 < 5(大小王除外);② 没有出现重复的值(大小王除外);

        判断是否出现重复的值可以借助 set 容器;

#include <iostream>
#include <vector>
#include <set>

class Solution {
public:
    bool isStraight(std::vector<int>& nums) {
        int max = -1, min = 14; // 初始化最大值和最小值
        for(int num : nums){
            if (num == 0) continue; // 跳过大小王
            if(S.find(num) != S.end()) return false;
            else S.insert(num);
            // 更新最大值和最小值
            max = std::max(max, num); 
            min = std::min(min, num);
            if ((max - min) >= 5) return false; 
        }
        return true;
    }
private:
    std::set<int> S;
};

int main(int argc, char *argv[]){
    Solution S1;
    std::vector<int> test = {1, 2, 3, 4, 5};
    bool res = S1.isStraight(test);
    if(res) std::cout << "true" << std::endl;
    else std::cout << "false" << std::endl;
    return 0;
}

2--圆圈中最后剩下的数字(62)

主要思路:

        

猜你喜欢

转载自blog.csdn.net/weixin_43863869/article/details/132053152