扑克牌顺子

扑克牌顺子

题目描述

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何。为了方便起见,你可以认为大小王是0。

// 扑克牌顺子.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
using namespace::std;

class Solution {
public:
	bool IsContinuous(vector<int> num) {
		if (num.empty() == true) return false ;

		QuickSort(num, 0, num.size() - 1) ;

		int countOfZero = 0 ;
		int hole = 0 ;

		for (vector<int>::iterator it = num.begin(); it != num.end(); ++it) {
			if (*it == 0) {
				++countOfZero ;
			}
		}

		for (int i = countOfZero + 1; i < num.size(); ++i) {
			int tmp = num[i] - num[i - 1] - 1 ;
			if (tmp == -1) return false ; // -1 意味着出现了对子
			hole += tmp ;
		}

		if (hole > countOfZero) return false ;
		else return true ;
	}

	void QuickSort(vector<int>& num, int l, int r) {
		int i = l ;
		int j = r ;
		int pivot = num[(i + j) / 2] ;

		while (i - j <= 0) {
			if (pivot > num[i]) ++i ;
			else if (pivot < num[j]) --j ;
			else {
				int tmp = num[i] ;
				num[i] = num[j] ;
				num[j] = tmp ;
				++i ; --j ;
			}
		}
		if (l < j) QuickSort(num, l, j) ;
		if (i < r) QuickSort(num, i, r) ;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	int arr[] = {1, 0, 0, 1, 0};
	vector<int> test(arr, arr + 5);
	Solution s;
	s.IsContinuous(test);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值