45. Poker Straight

Problem solving ideas

         LL is in a very good mood today, because he went to buy a deck of playing cards and found that there were 2 big kings and 2 little kings in it (a deck of cards was originally 54 cards ^_^)...

        He randomly drew 5 cards from it, and wanted to test his luck to see if he could draw a straight. If he did, he decided to buy a sports lottery ticket, hehe! ! "Ace of Hearts, 3 of Spades, King, King, 5 of Diamonds", "Oh My God!" is not a straight....

      LL was not happy, he thought about it, and decided that the big and small kings 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 become "1, 2, 3, 4, 5" (the big and small kings are regarded as 2 and 4 respectively), "So Lucky!". LL decided to buy a sports lottery ticket. Now, you are asked to use this card to simulate the above process, and then tell us how lucky LL is.

          For convenience, you can consider the big and small kings to be 0.

Problem solving ideas

    public boolean isContinuous(int[] numbers) {
        if (numbers.length != 5) return false;
        int min = 14;
        int max = -1;
        int flag = 0;
        for (int i = 0; i < numbers.length; i++) {
            int number = numbers[i];
            if (number < 0 || number > 13) return false;
            if (number == 0) continue;
            if (((flag >> number) & 1) == 1) return false;
            flag |= (1 << number);
            if (number > max) max = number;
            if (number < min) min = number;
            if (max - min >= 5) return false;
        }
        return true;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325905107&siteId=291194637