题单介绍:

精选 100 道力扣(LeetCode)上最热门的题目,适合初识算法与数据结构的新手和想要在短时间内高效提升的人,熟练掌握这 100 道题,你就已经具备了在代码世界通行的基本能力。

目录

题单介绍:

题目:139. 单词拆分 - 力扣(Leetcode)

题目的接口:

解题思路:

代码:

过过过过啦!!!!

题目:146. LRU 缓存 - 力扣(Leetcode)

题目的接口:

解题思路:

代码:

过过过过啦!!!!

写在最后:


题目:139. 单词拆分 - 力扣(Leetcode)

【LeetCode】HOT 100(17)-LMLPHP

题目的接口:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {

    }
};

解题思路:

我一开始的思路是用哈希,

先让字符串列表进哈希,然后遍历字符串,

如果遍历到最后,用来对比的临时数组为空,就证明都匹配完了,

结果只过了一部分样例,一看题解,动态规划,这里贴出代码:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> st;
        for(auto e : wordDict) st.insert(e);
        int left = 0;
        string tmp;
        for(int i = 0; i < s.size(); i++) {
            tmp += s[i];
            if(st.find(tmp) != st.end()) {
                tmp.clear();
            }
        }
        if(tmp == "") return true;
        return false;
    }
};

杀了我吧,这是官解的代码:

等我学完动态规划,教他做人

代码:

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> st;
        for(auto e : wordDict) st.insert(e);

        vector<bool> dp(s.size() + 1);
        dp[0] = true;
        for(int i = 0; i <= s.size(); i++) {
            for(int j = 0; j < i; j++) {
                if(dp[j] && st.find(s.substr(j, i - j)) != st.end()) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.size()];
    }
};

过过过过啦!!!!

【LeetCode】HOT 100(17)-LMLPHP

题目:146. LRU 缓存 - 力扣(Leetcode)

【LeetCode】HOT 100(17)-LMLPHP

题目的接口:

class LRUCache {
public:
    LRUCache(int capacity) {

    }
    
    int get(int key) {

    }
    
    void put(int key, int value) {

    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

解题思路:

这道题的思路其实不难,

就是写起来太恶心了,

具体思路就是用一个哈希来存值,

用一个双向带头循环链表来修改顺序,

有数插入就根据情况修改KV值,修改优先级就行,

代码如下:

代码:

struct DLinkedNode {
    int key, value;
    DLinkedNode* prev;
    DLinkedNode* next;
    DLinkedNode(): key(0), value(0), prev(nullptr), next(nullptr) {}
    DLinkedNode(int _key, int _value): key(_key), value(_value), prev(nullptr), next(nullptr) {}
};

class LRUCache {
private:
    unordered_map<int, DLinkedNode*> cache;
    DLinkedNode* head;
    DLinkedNode* tail;
    int size;
    int capacity;

public:
    LRUCache(int _capacity): capacity(_capacity), size(0) {
        // 使用伪头部和伪尾部节点
        head = new DLinkedNode();
        tail = new DLinkedNode();
        head->next = tail;
        tail->prev = head;
    }
    
    int get(int key) {
        if (!cache.count(key)) {
            return -1;
        }
        // 如果 key 存在,先通过哈希表定位,再移到头部
        DLinkedNode* node = cache[key];
        moveToHead(node);
        return node->value;
    }
    
    void put(int key, int value) {
        if (!cache.count(key)) {
            // 如果 key 不存在,创建一个新的节点
            DLinkedNode* node = new DLinkedNode(key, value);
            // 添加进哈希表
            cache[key] = node;
            // 添加至双向链表的头部
            addToHead(node);
            ++size;
            if (size > capacity) {
                // 如果超出容量,删除双向链表的尾部节点
                DLinkedNode* removed = removeTail();
                // 删除哈希表中对应的项
                cache.erase(removed->key);
                // 防止内存泄漏
                delete removed;
                --size;
            }
        }
        else {
            // 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
            DLinkedNode* node = cache[key];
            node->value = value;
            moveToHead(node);
        }
    }

    void addToHead(DLinkedNode* node) {
        node->prev = head;
        node->next = head->next;
        head->next->prev = node;
        head->next = node;
    }
    
    void removeNode(DLinkedNode* node) {
        node->prev->next = node->next;
        node->next->prev = node->prev;
    }

    void moveToHead(DLinkedNode* node) {
        removeNode(node);
        addToHead(node);
    }

    DLinkedNode* removeTail() {
        DLinkedNode* node = tail->prev;
        removeNode(node);
        return node;
    }
};

过过过过啦!!!!

【LeetCode】HOT 100(17)-LMLPHP

写在最后:

以上就是本篇文章的内容了,感谢你的阅读。

如果感到有所收获的话可以给博主点一个哦。

如果文章内容有遗漏或者错误的地方欢迎私信博主或者在评论区指出~

06-20 17:58