leetcode轮回计划20181224

  1. 201 Bitwise AND of Numbers Range
    题意:闭区间所有数值求AND
    思路:分治递归
  2. 207 Course Schedule
    题意:先修课依赖
    思路:BFS
  3. 208 Implement Trie (Prefix Tree)
    题意:前缀树
struct Node{
    Node* dic[26];
    bool isWord;
    Node(){
        for(int i = 0;i < 26;++ i){
            dic[i] = NULL;
        }
        isWord = false;
    }
};

class Trie {
public:
    Node* root;
    Trie() {
        root = new Node();
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Node* i = root;
        for(auto a : word){
            int c = a - 'a';
            if(i->dic[c] == NULL){
                i->dic[c] = new Node();
            }
            i = i->dic[c];
        }
        i->isWord = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Node* i = root;
        for(auto a : word){
            int c = a - 'a';
            if(i->dic[c] == NULL){
                return false;
            }
            i = i->dic[c];
        }
        return i->isWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Node* i = root;
        for(auto a : prefix){
            int c = a - 'a';
            if(i->dic[c] == NULL){
                return false;
            }
            i = i->dic[c];
        }
        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * bool param_2 = obj.search(word);
 * bool param_3 = obj.startsWith(prefix);
 */
  1. 209 Minimum Size Subarray Sum
    题意:不小于某数字的最短子数列
    思路:DP
  2. 210 Course Schedule II
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。