Poker straight wins the offer-

Title Description

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

Sorting, counting the number of 0, it is determined whether the needs 0, determines whether or not the sub occurred

AC Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    bool IsContinuous(vector<int> numbers)
    {
        if (numbers.size()==0)
            return false;
        sort(numbers.begin(), numbers.end());
        int zero_size = 0;
        int i = 0;
        for (; i < numbers.size(); i++)
        {
            if (numbers[i] == 0)
                zero_size++;
            else
                break;
        }
        for (int j = i; j < numbers.size()-1; j++)
        {
            if (numbers[j] == numbers[j + 1]) //出现对子
                return false;
            int diff = numbers[j + 1] - numbers[j] - 1;
            if (diff > zero_size)
                return false;
            else
            {
                zero_size -= diff;
            }
        }
        return true;
    }
};
int main()
{
    vector<int> data = {0, 3, 4, 5, 0};
    Solution so;
    cout << so.IsContinuous(data) << endl;

    return 0;
}
Published 58 original articles · won praise 7 · views 2677

Guess you like

Origin blog.csdn.net/weixin_42100456/article/details/104493109