zoukankan      html  css  js  c++  java
  • 扑克牌中的顺子

    排序:

    class Solution {
        public boolean isStraight(int[] nums) {
            Arrays.sort(nums);
            int zero = 0;
            int needZero = 0;
            if(nums[0] == 0) zero++;
            for(int i=1;i<nums.length;i++){
                if(nums[i] - nums[i-1] > 1&&nums[i-1]>0){
                    needZero += (nums[i]-nums[i-1]-1);
                }
                if(nums[i] == nums[i-1] && nums[i]>0){
                    return false;
                }
                if(nums[i] == 0) zero++;
            }
            return zero >= needZero;
        }
    }
    

    不排序

    class Solution {
        public boolean isStraight(int[] nums) {
            int[] hash = new int[14];
            int endIndex = 13,startIndex=1;
            int needZero=0;
            for(int num:nums){
                hash[num]++;
            }
            while(hash[endIndex]==0) endIndex--;//确定顺子的最大下标
            while(hash[startIndex]==0)startIndex++;//确定顺子的最小下标
            for(int i=endIndex;i>=startIndex;i--){//中间少的用零补
                if(hash[i]>1&&i>0) return false;
                if(hash[i]==0&&i>0) needZero++;
            }
            return needZero<=hash[0];
        }
    }
    
    
    不一样的烟火
  • 相关阅读:
    selenium之css选择器高级用法
    常系数线性齐次递推新理解
    关于莫队本质的理解
    2021.5.8总结
    决策单调性优化dp
    字符串 复习
    5.1总结
    树分块 学习笔记
    莫反 复习
    P4570 [BJWC2011]元素
  • 原文地址:https://www.cnblogs.com/cstdio1/p/13377104.html
Copyright © 2011-2022 走看看