Cattle off the net - play poker straight wins the office-

Topic : 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, Cece want your own 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.
Idea : using the set container statistics whether there have been, if there have been direct returns false. Through the array, 0 is skipped, the digital array of the maximum and minimum statistics, if the difference between the two is less than or equal to 4, i.e., may be formed straight, returns true, otherwise returns false.

class Solution {
public:
    bool IsContinuous( vector<int> numbers ) {
        if (numbers.empty()) return false;
        int nmin =INT_MAX;
        int nmax = INT_MIN;
        set<int> nset;
        for(int i=0;i<numbers.size();++i)
        {
            if(numbers[i]==0)
                continue;
            else
            {
                if(nset.count(numbers[i]))
                    return false;
                else
                    nset.insert(numbers[i]);
                if(numbers[i]<nmin)
                    nmin=numbers[i];
                if(numbers[i]>nmax)
                    nmax=numbers[i];
            }
        }
        int diff=abs(nmax-nmin);
        if(diff<=4) return true;
        else
            return false;
    }
};

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/91351586