Offer to prove safety forty-five: poker straight

Casual working

LL particularly good mood today, because he bought a deck of cards and found that there actually has two king, two small king (the deck was originally 54 _ ) ... he randomly drawn from the five cards, I would like to test test their luck, see if you can be able to get straight, if able to get it, he decided to buy a sports lottery, hey! ! "Red A, 3 of spades, Wang, King, square piece 5", "Oh My God! " Is not a straight ... LL happy, he thought, decides \ Amy can be seen as any number, and A regarded as 1, J is 11, Q is 12, K 13. The above five cards can become "1,2,3,4,5" (king size and 2, respectively seen as 4), "So Lucky!" . LL decided to buy sports lottery friends. Now, I ask you to use this piece of card simulate the above procedure, and then tell us how LL luck, if the card can be composed of straight to output true, otherwise it will output false. For convenience, you can think of king size is 0.

Thinking

For this problem 0 means universal, if the number is 0 for four on the fifth number is nothing to meet the conditions, if the number 0 is three, for the remaining two, if the difference is no more than 4, for a given number three is to meet, so that we can first sort, as long as the maximum and minimum values ​​no more than 4 times, all to meet the condition of our requirements. If the number 0 is 0, a traversal proceeds traverses two queries whether the difference value is not 1.

Code

  public boolean isContinuous(int [] num) {
        if(num.length==0)
            return false;
        Arrays.sort(num);
        if(num[3]==0) return true;
        if(num[2]==0){
            if(1<=num[4]-num[3]&&num[4]-num[3]<=4)
                return true;
         return false;
        }
        if(num[1]==0){
            if(1<=num[4]-num[2]&&num[4]-num[2]<=4)
                return true;
            return false;
        }
        if(num[0]==0){
            if(1<=num[4]-num[1]&&num[4]-num[1]<=4)
                return true;
            return false;
        }
 
        for(int i=0;i<4;i++)
            if(num[i]+1!=num[i+1])
                return false;
       return true;
         
    }
Published 68 original articles · won praise 39 · views 5020

Guess you like

Origin blog.csdn.net/weixin_44015043/article/details/105392144