leetcode 每日一题 464. 我能赢吗
作者:互联网
leetcode 每日一题 464. 我能赢吗
class Solution { Map<Integer, Boolean> memo = new HashMap<Integer, Boolean>(); public boolean canIWin(int maxChoosableInteger, int desiredTotal) { if ((1 + maxChoosableInteger) * (maxChoosableInteger) / 2 < desiredTotal) { return false; } return dfs(maxChoosableInteger, 0, desiredTotal, 0); } public boolean dfs(int maxChoosableInteger, int usedNumbers, int desiredTotal, int currentTotal) { if (!memo.containsKey(usedNumbers)) { boolean res = false; for (int i = 0; i < maxChoosableInteger; i++) { if (((usedNumbers >> i) & 1) == 0) { if (i + 1 + currentTotal >= desiredTotal) { res = true; break; } if (!dfs(maxChoosableInteger, usedNumbers | (1 << i), desiredTotal, currentTotal + i + 1)) { res = true; break; } } } memo.put(usedNumbers, res); } return memo.get(usedNumbers); } }
标签:int,464,我能,dfs,maxChoosableInteger,desiredTotal,boolean,usedNumbers,leetcode 来源: https://www.cnblogs.com/yexuba/p/16354363.html