其他分享
首页 > 其他分享> > 464. 我能赢吗

464. 我能赢吗

作者:互联网

class Solution {

	map<int, bool>m;
public:
	bool win(int max, int target, int visited)
	{
		if (m.find(visited) != m.end())
			return m[visited];
		for (int i = 1; i <= max; i++)
		{
			int flag = (1 << i);
			if ((visited & flag) == 0 && ((i >= target) || win(max, target - i, visited | flag) == false))
			{
				m[visited] = true;
				return true;
			}

		}
		m[visited] = false;
		return false;
	}


	bool canIWin(int maxChoosableInteger, int desiredTotal) {
		int num = maxChoosableInteger;
		if (num >= desiredTotal)
			return true;
		int sum = (1 + num)*num / 2;
		if (sum < desiredTotal)
			return false;
		return win(num, desiredTotal, 0);
	}
};

 

标签:return,int,464,我能,desiredTotal,num,visited,false
来源: https://blog.csdn.net/Scarlett_Guan/article/details/99186256