JZ45 Poker Straight

Title description

LL was in a very good mood today, because he bought a deck of playing cards and found that there were 2 big kings and 2 little kings in it (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, Spades 3, Little King, Big King, Square Piece 5", "Oh My God!" 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 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.

Code

import java.util.Arrays;
public class Solution {
    
    
   public boolean isContinuous(int [] numbers) {
    
    
        Arrays.sort(numbers);
        int zorecount = 0;//0的个数
        int sum =0;//非零元素之间差的和
        int[] narr = new int[14];
       if(numbers.length == 0){
    
    
           return false;
       }
        //计算0的个数
        for (int i = 0; i < numbers.length; i++) {
    
    
            if (numbers[i] == 0){
    
    
                zorecount++;
            }
        }
        //判断非0重复元素情况
        for (int i = 0; i < numbers.length; i++) {
    
    
            narr[numbers[i]]++;
            if (numbers[i] !=0 && narr[numbers[i]] > 1) {
    
    
                return false;
            }

        }
        //判断无大小王的情况
        for (int i = 1; i < numbers.length; i++) {
    
    
            if (numbers[0] != 0 && numbers[i] - numbers[i-1] != 1) {
    
    
                return false;
            }
        }
        //判断有大小王的情况
        if (numbers[0] == 0) {
    
    
            //计算非零元素之间差的和
            for (int i = numbers.length-1; i > 0; i--) {
    
    
                if(numbers[i] != 0 && numbers[i] - numbers[i-1] != numbers[i]){
    
    
                    sum += numbers[i] - numbers[i-1] -1;
                }
            }
            if (zorecount < sum  ) {
    
    
                return  false;
            }
        }
        return true;

    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/108501111