846. 一手顺子

846. 一手顺子

解题思路:根据题目要求,将数组从小到大排序,已hand[0],为起点,组成大小为W的顺子;

 

class Solution {
public:
	bool isNStraightHand(vector<int>& hand, int W) 
	{
		sort(hand.begin(), hand.end());
		while (hand.size()!=0)
		{
			if (dispose(hand, hand[0], W))
			{
				continue;
			}
			else
				return false;
		}
		return true;
	}
	bool dispose(vector<int>& hand, int begin, int num)
	{
		int cnt = 0;
		auto pos = find(hand.begin(), hand.end(), begin);
		while (cnt<num)
		{
			if (pos==hand.end())
			{
				return false;
			}
			hand.erase(pos);
			begin++;
			pos = find(hand.begin(), hand.end(), begin);
			cnt++;
		}
		return true;
	}
};

猜你喜欢

转载自blog.csdn.net/kongqingxin12/article/details/83386431