LeetCode 刷题笔记
本人刷题的笔记LeetCode 146. LRU 缓存C146. LRU 缓存class node{ public: int key; int val; node* pre; node* next; node(int key, int val){ this-key key; this-val val; pre nullptr; next nullptr; } }; class LRUCache { private: int cap; unordered_mapint, node* map; node* haed; node* tail; public: LRUCache(int capacity) { cap capacity; haed new node(0, 0); tail new node(0, 0); haed-next tail; tail-pre haed; } int get(int key) { if(map.count(key)){ remove(map[key]); insertHead(map[key]); return map[key]-val; } else{ return -1; } } void put(int key, int value) { if(map.count(key)){ remove(map[key]); insertHead(map[key]); map[key]-val value; } else{ node* nn new node(key, value); insertHead(nn); map[key] nn; } if(map.size() cap){ node* hou tail-pre; remove(hou); map.erase(hou-key); delete hou; } } void insertHead(node* val){ node* t haed-next; haed-next val; val-pre haed; val-next t; t-pre val; } void remove(node* val){ node* vpre val-pre; node* vnext val-next; vpre - next vnext; vnext - pre vpre; } };LeetCode 438. 找到字符串中所有字母异位词class Solution { public: vectorint findAnagrams(string s, string p) { int left 0, right 0; int count 0; unordered_mapchar, int p_map, temp_map; vectorint res; if(s.size() p.size()){ return res; } for(auto num:p){ p_map[num]; } while(right s.size()){ char r s[right]; right; if(p_map.count(r)) { temp_map[r]; if(p_map[r] temp_map[r]){ count; } } //改成 if(right - left p.size())也行 while(right - left p.size()){ if(count p_map.size()){ res.push_back(left); } char l s[left]; left; if(p_map.count(l)){ if(temp_map[l] p_map[l]){ count--; } temp_map[l]--; } } } return res; } };