• Leetcode 139 单词拆分


      JAVA DP:

    public final boolean wordBreak(String s, List<String> wordDict) {
            Set<String> set = new HashSet<String>();
            for (String word : wordDict) set.add(word);
            return search(s, set, new HashMap<String, Boolean>());
        }
    
        private final boolean search(String s, Set<String> wordDict, Map<String, Boolean> cache) {
            if (cache.keySet().contains(s)) return cache.get(s);
            int len = s.length();
            boolean res = false;
            for (int i = 0; i <= len; i++) {
                String pre = s.substring(0, i);
                if (!wordDict.contains(pre)) continue;
                if (search(s.substring(i, len), wordDict, cache) || i == len) {
                    res = true;
                    break;
                }
            }
            cache.put(s, res);
            return res;
        }

      JS DP:

    /**
     * @param {string} s
     * @param {string[]} wordDict
     * @return {boolean}
     */
    var wordBreak = function (s, wordDict) {
        let wordDictSet = new Set();
        for (let i = 0; i < wordDict.length; i++) wordDictSet.add(wordDict[i]);
        return search(s, wordDictSet, new Map());
    };
    
    var search = function (s, wordDict, cache) {
        if (cache.get(s) !== undefined) return cache.get(s);
        let len = s.length, res = false;
        for (let i = 0; i <= len; i++) {
            let pre = s.substring(0, i);
            if (!wordDict.has(pre)) continue
            if (search(s.substring(i, len), wordDict, cache) || i == len) {
                res = true;
                break;
            }
        }
        cache.set(s, res);
        return res;
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    用Sqoop进行Hive和MySQL之间的数据互导
    Spark读HBase写MySQL
    Kafka如何彻底删除topic及数据
    LDAP-HA安装与配置(Keepalived方式实现)
    配置两个Hadoop集群Kerberos认证跨域互信
    MYSQL HA 部署手册
    ELK简单安装测试
    Elasticsearch CURL命令
    大数据常见错误解决方案(转载)
    生成 RSA 公钥和私钥的方法
  • 原文地址:https://www.cnblogs.com/niuyourou/p/15009021.html
Copyright © 2020-2023  润新知