# 面试题61

从扑克牌中随机抽5张牌，判断是不是一个顺子，即这5张牌是不是连续的。2～10为数字本身，A为1，J为11，Q为12，K为13，而大、小王为 0 ，可以看成任意数字。A 不能视为 14。

```
示例 1:

输入: [1,2,3,4,5]
输出: True



示例 2:

输入: [0,0,1,2,5]
输出: True
```

## 限制：

* 数组长度为 5
* 数组的数取值为 \[0, 13] .

## Solutions

1. **counter**

```cpp
class Solution {
public:
    bool isStraight(vector<int>& nums) {
        int arr[15] = {0};
        int minn = INT_MAX, maxn = INT_MIN, filled = 0;
        for (auto & n : nums) {
            if (n != 0) {
                if (++arr[n] == 2)
                    return false;
                minn = min(minn, n);
                maxn = max(maxn, n);
            }
            filled++;
        }
        return filled >= maxn - minn + 1;
    }
};
```

* Or use sort.
* reference: [https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/solution/ji-bai-shuang-bai-by-adonis-7/](https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/solution/ji-bai-shuang-bai-by-adonis-7/)

```cpp
class Solution {
public:
    bool isStraight(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int numzero = 0;

        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] == 0)
                numzero++;
            else if (nums[i] == nums[i + 1])
                return false;
            else if (nums[i] + 1 != nums[i + 1])
                numzero -= nums[i + 1] - nums[i] - 1;
        }

        return numzero >= 0;
    }
};
```
