leetcode 剑指Offer 61.扑克牌中的顺子 Java

做题博客链接

https://blog.csdn.net/qq_43349112/article/details/108542248

题目链接

https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/

描述

从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。210为数字本身,A为1,J为11,Q为12,
K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。

限制:

数组长度为 5 

数组的数取值为 [0, 13] .

示例

示例 1:

输入: [1,2,3,4,5]
输出: True

示例 2:

输入: [0,0,1,2,5]
输出: True

初始代码模板

class Solution {
    public boolean isStraight(int[] nums) {

    }
}

代码

推荐题解:
https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/solution/mian-shi-ti-61-bu-ke-pai-zhong-de-shun-zi-ji-he-se/

class Solution {
    public boolean isStraight(int[] nums) {
        int[] arr = new int[14];
        int min = 14;
        int max = -1;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                continue;
            } else {
                if (arr[nums[i]] == 1) {
                    return false;
                }
                arr[nums[i]]++;
                min = Math.min(min, nums[i]);
                max = Math.max(max, nums[i]);
            }
        }

        return max - min < 5;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值