【Array】Poker Straight

Title description

LL was in a very good mood today, because he went to buy a deck of playing cards and found that there were actually 2 big kings and 2 little kings (the deck of cards was originally 54 _ )... He randomly drew 5 cards from it and wanted to test it. Test his luck and see if he can draw a straight. If he can draw, he decides to buy a sports lottery, hehe! ! "Heart Ace, 3 of Spades, Little King, Big King, Square Piece 5", "Oh My God!" It's not a straight... LL is not happy, he thought about it, and decided that Big \ Little King can be regarded as any number, and A is regarded as 1, J is 11, Q is 12, and K is 13. The 5 cards above can be turned into "1,2,3,4,5" (big and small kings are regarded as 2 and 4 respectively), "So Lucky!". LL decided to buy a sports lottery. Now, you are asked to use this card to simulate the above process, and then tell us how lucky the LL is. If the card can form a straight, it will output true, otherwise it will output false. For convenience, you can consider the size king to be 0.

Example 1
input
[0,3,2,6,4]

Return value
true


Question understanding: This question means that given an array, containing 5 numbers (may be empty), 0 represents the king, similar to the scorpion, can replace any number, you need to judge whether this array can form a straight, for example { 0, 3, 2, 6, 4}, 0 can replace 5 to form a straight of 2, 3, 4, 5, 6.

method one

First sort the array, count the number of scorpions, start traversing from a position that is not 0, and judge whether a straight can be formed. If the position cannot be connected, then judge whether it can be replaced with scorpions, and if so, continue to judge the next position Can it be connected? If you encounter a pair, just return false

import java.util.Arrays;

public class Solution {
    
    
    public boolean IsContinuous(int[] numbers) {
    
    
        if (numbers.length == 0) return false;
        Arrays.sort(numbers);
        // 统计癞子的个数
        int count0 = 0;
        for (int i = 0; i < numbers.length; i++) {
    
    
            if (numbers[i] == 0) count0++;
            else break;
        }
        if (count0 == 4) return true;
		// 从第一个不为0的牌开始,last代表上一张牌
        int index = count0 + 1, last = numbers[count0];

        while (count0 > 0 || index < numbers.length) {
    
    
            if (numbers[index] != last + 1) {
    
    
                if (count0 > 0) {
    
    
                    count0--; // 癞子数减一
                    last = last + 1; // 癞子顶替需要连上的牌,更新last的值
                } else {
    
    
                	// 没有癞子顶替,无法组成顺子
                    return false;
                }
            } else if (numbers[index] == last) {
    
    
                // 对子
                return false;
            } else {
    
    
            	// 更新last的值
                last = numbers[index];
                index++; // 成功连上,判断下一张牌能否和last连上
            }
        }
        return true;
    }
}

Method Two

Sort the array first, traverse the array, and count the number of scorpions count0. If you encounter a pair, return false directly. Otherwise, the number of intervals between the two cards before and after the accumulation will be accumulated. The interval is equal to 1 for connecting, and greater than 1 means that the scorpion needs to be replaced here. If the number of intervals is greater than the number of scorpions, you cannot make a straight.

import java.util.Arrays;
public class Solution {
    
    
    public boolean IsContinuous(int[] numbers) {
    
    
        if (numbers.length == 0) return false;
        Arrays.sort(numbers);
        int count0 = 0, interval = 0;
        for (int i = 0; i < numbers.length - 1; i++) {
    
    
            if (numbers[i] == 0)
                count0++;
            else if (numbers[i + 1] == numbers[i])
                return false;
            else
                interval += numbers[i + 1] - numbers[i] - 1;
        }
        return interval > count0 ? false : true;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43486780/article/details/113818723