码迷,mamicode.com
首页 > Windows程序 > 详细

464 Can I Win 我能赢吗

时间:2018-04-21 19:34:23      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:https   integer   ++   problem   logs   for   sed   desire   can   

详见:https://leetcode.com/problems/can-i-win/description/

C++:

class Solution {
public:
    bool canIWin(int maxChoosableInteger, int desiredTotal)
    {
        if (maxChoosableInteger >= desiredTotal)
        {
            return true;
        }
        if (maxChoosableInteger * (maxChoosableInteger + 1) / 2 < desiredTotal) 
        {
            return false;
        }
        unordered_map<int, bool> m;
        return canWin(maxChoosableInteger, desiredTotal, 0, m);
    }
    bool canWin(int length, int total, int used, unordered_map<int, bool>& m) 
    {
        if (m.count(used))
        {
            return m[used];
        }
        for (int i = 0; i < length; ++i) 
        {
            int cur = (1 << i);
            if ((cur & used) == 0) 
            {
                if (total <= i + 1 || !canWin(length, total - (i + 1), cur | used, m)) 
                {
                    m[used] = true;
                    return true;
                }
            }
        }
        m[used] = false;
        return false;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/6103525.html

464 Can I Win 我能赢吗

标签:https   integer   ++   problem   logs   for   sed   desire   can   

原文地址:https://www.cnblogs.com/xidian2014/p/8902469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!