zoukankan      html  css  js  c++  java
  • leetcode 464. Can I Win

    464. Can I Win

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

    用递归的方式进行搜索,用hash表减少搜索的次数。因为数字不超过20,所以可以用一个int的位来表示是否访问过。

    注意:

    (cur & used) == 0这个地方必须加括号,不然就报错了。

    class Solution {
    public:
        bool canIWin(int maxChoosableInteger, int desiredTotal) {
            if(maxChoosableInteger >=  desiredTotal)
                return true;
            if((1 + maxChoosableInteger) * maxChoosableInteger/2 < desiredTotal)
                return false;
            unordered_map<int,bool> m;
            return canIWin(maxChoosableInteger,desiredTotal,0,m);
        }
        bool canIWin(int maxChoosableInteger, int desiredTotal,int used,unordered_map<int,bool>& m){
            if(m.find(used) != m.end())
                return m[used];
            for(int i = 1;i <= maxChoosableInteger;i++){
                int tmp = 1 << (i-1);
                if((tmp&used) == 0){
                    if(desiredTotal <= i || !canIWin(maxChoosableInteger,desiredTotal - i,used|tmp,m)){
                        m[used] = true;
                        return true;
                    }
                }
            }
            m[used] = false;
            return false;
        }
    };
  • 相关阅读:
    web项目优化
    mysql 优化笔记
    Java 调用 google 翻译
    Git回滚merge操作
    mybatis 批量插入 返回主键id
    idea tomcat debug 失效
    mysql 常用语句
    xstream 解析xml报文
    activeMQ 讲解及实战
    linux svn apache
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/10968322.html
Copyright © 2011-2022 走看看