"Swords Offer" "Poker Straight"

Topic description


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, He 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!" It's not Shunzi... LL is not happy, he thinks about it, and decides that the big\xiaowang can be regarded as any number, and A is regarded as 1, J is 11, Q is 12, and K is 12. 13. The above 5 cards 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 simulate the above process with this card and tell us how lucky LL is. For convenience, you can consider the big and small kings to be 0.

Code


//思路 排先序,拿到非0的数组,相减之差<5
class Solution {
public:
    bool IsContinuous( vector<int> numbers ) {
        int size = numbers.size();
        if (size <  5 ){
            return false;
        }
        sort(numbers.begin(),numbers.end());
        int i = 0;
        int all = 0;
        while(i< 5){
            if(numbers[i]== 0 ){
                all++;
            }
            i++;
        }
        bool res =true;
        for(int j =all ;j< 4;j++){
            if(numbers[j+1] - numbers[j] == 0){
                 res =false;
                 break;
            }
           else if(numbers[j+1] - numbers[j] == 1){

            }  
            else if(numbers[j+1] - numbers[j] == 2)
            {
                if (all < 1){
                    res =false;
                    break;
                }
                else{
                   all --;
                }
            }
            else if(numbers[j+1] - numbers[j] == 3){
                if (all < 2){
                    res =false;
                    break;
                }
                else{
                    all --;
                    all --;

                }
            }
            else if(numbers[j+1] - numbers[j] == 4){
                if (all < 3){
                    res =false;
                    break;
                }
                else{
                    all --;
                    all --;
                    all --;
                }
            }
            else{
                res =false;
                    break;
            }
        }
        return res;

    }
};

Guess you like

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