LeetCode刷题笔记(2022-05) LeetCode刷题笔记(2022-06) LeetCode刷题笔记(2022-07,08) LeetCode刷题笔记(2022-09) LeetCode刷题笔记(2022-11) [543] 二叉树的直径 Time: 2022-07-10
Difficulty: Easy (57.21%)
Tags: tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 class Solution {#ifndef max #define max(x, y) x > y ? x : y #endif private : int maxDiameter = 0 ; void dfsCalc (TreeNode *root) { if (!root) return ; dfsCalc (root->left); dfsCalc (root->right); int leftLength = root->left ? root->left->val + 1 : 0 ; int rightLength = root->right ? root->right->val + 1 : 0 ; root->val = max (leftLength, rightLength); maxDiameter = max (maxDiameter, leftLength + rightLength); } public : int diameterOfBinaryTree (TreeNode *root) { if (!root) return 0 ; dfsCalc (root); return maxDiameter; } };
[560] 和为 K 的子数组 Time: 2022-07-27
Difficulty: Medium (45.38%)
Tags: array | hash-table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Solution {public : int subarraySum (vector<int > &nums, int k) { map<int , int > preSumFreq; preSumFreq[0 ] = 1 ; int preSum = 0 ; int count = 0 ; for (auto num : nums) { preSum += num; if (preSumFreq.find (preSum - k) != preSumFreq.end ()) count += preSumFreq[preSum - k]; preSumFreq[preSum] = preSumFreq[preSum] + 1 ; } return count; } };
[581] 最短无序连续子数组 Time: 2022-07-28
Difficulty: Medium (41.34%)
Tags: array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 class Solution {public : int findUnsortedSubarray (vector<int > &nums) { if (nums.size () < 2 ) return 0 ; int Max = INT_MIN; int Min = INT_MAX; int R = 0 ; int L = 0 ; for (int i = 0 ; i < nums.size (); i++) { if (Max > nums[i]) R = i; Max = max (Max, nums[i]); } for (int i = nums.size () - 1 ; i >= 0 ; i--) { if (Min < nums[i]) L = i; Min = min (Min, nums[i]); } return R == L ? 0 : R - L + 1 ; } };
[621] 任务调度器 Time: 2022-07-29
Difficulty: Medium (58.65%)
Tags: array | greedy | queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 class Solution {private : inline char findMostChar (map<char , int > &hash) { auto mostChar = 'A' ; for (auto i = 'A' ; i <= 'Z' ; i++) { if (hash[i] > hash[mostChar]) { mostChar = i; } } return mostChar; } public : int leastInterval (vector<char > &tasks, int n) { map<char , int > hash; for (auto task : tasks) { hash[task]++; } char mostChar; int length = 0 , mostTasks = 0 ; for (; hash[mostChar = findMostChar (hash)];) { if (!mostTasks) { mostTasks = hash[mostChar]; length = hash[mostChar] * (n + 1 ) - n; } else { if (hash[mostChar] == mostTasks) { length++; } } hash[mostChar] = 0 ; } return max (length, (int )tasks.size ()); } };
[739] 每日温度 Time: 2022-08-04
Difficulty: Medium (69.41%)
Tags: hash-table | stack
看了 Tags 就懂了😓
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution {public : vector<int > dailyTemperatures (vector<int > &temperatures) { auto answer = vector <int >(temperatures.size (), 0 ); auto stack = std::stack <int >(); stack.push (0 ); for (int i = 1 ; i < temperatures.size (); i++) { for (; !stack.empty () && temperatures[i] > temperatures[stack.top ()];) { answer[stack.top ()] = i - stack.top (); stack.pop (); } stack.push (i); } return answer; } };
[1408] 数组中的字符串匹配 Time: 2022-08-07
Difficulty: Easy (64.33%)
Tags: Unknown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution {public : vector<string> stringMatching (vector<string> &words) { auto ans = vector <string>(); for (int i = 0 ; i < words.size (); i++) { for (int j = 0 ; j < words.size (); j++) { if (i == j) continue ; if (words[i].length () > words[j].length ()) continue ; if (words[j].find (words[i]) != string::npos) { ans.push_back (words[i]); break ; } } } return ans; } };
[636] 函数的独占时间 Time: 2022-08-13
Difficulty: Medium (65.82%)
Tags: stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 class MySolution {private : vector<string> split (const string &s, char delim) { vector<string> result; stringstream ss (s) ; string item; while (getline (ss, item, delim)) { result.push_back (item); } return result; } inline tuple<int , bool , int > logHandler (string log) { auto strs = split (log, ':' ); return {stoi (strs[0 ]), strs[1 ] == "start" , stoi (strs[2 ])}; } public : vector<int > exclusiveTime (int n, vector<string> &logs) { vector<int > ans (n, 0 ) ; stack<pair<int , int >> stk; for (auto log : logs) { auto [functionId, isStart, time] = logHandler (log); if (stk.empty ()) { stk.push ({functionId, time}); continue ; } auto [fId, T] = stk.top (); if (isStart) { ans[fId] += time - T; stk.push ({functionId, time}); } else { ans[functionId] += time - T + 1 ; stk.pop (); if (!stk.empty ()) { stk.top ().second = time + 1 ; } } } return ans; } };
官方做法,解决思路一样,但是使用的轮子比我快很多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 class Solution {public : vector<int > exclusiveTime (int n, vector<string> &logs) { stack<pair<int , int >> st; vector<int > res (n, 0 ) ; for (auto &log : logs) { char type[10 ]; int idx, timestamp; sscanf (log.c_str (), "%d:%[^:]:%d" , &idx, type, ×tamp); if (type[0 ] == 's' ) { if (!st.empty ()) { res[st.top ().first] += timestamp - st.top ().second; st.top ().second = timestamp; } st.emplace (idx, timestamp); } else { auto t = st.top (); st.pop (); res[t.first] += timestamp - t.second + 1 ; if (!st.empty ()) { st.top ().second = timestamp + 1 ; } } } return res; } };
[641] 设计循环双端队列 Time: 2020-08-15
Difficulty: Medium (53.15%)
Tags: Unknown
用数组更快?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 class MyCircularDeque {private : int maxNum; int num; struct DqNode { int val; DqNode *next; DqNode *before; DqNode (int val, DqNode *next, DqNode *before) : val (val), next (next), before (before){}; }; DqNode *front; DqNode *rear; public : MyCircularDeque (int k) { this ->maxNum = k; this ->num = 0 ; front = nullptr ; rear = nullptr ; } ~MyCircularDeque () { for (; front;) { auto tmp = front; front = front->next; delete tmp; } } bool insertFront (int value) { if (num == maxNum) { return false ; } if (num == 0 ) { front = new DqNode (value, nullptr , nullptr ); rear = front; } else { front->before = new DqNode (value, front, nullptr ); front = front->before; } this ->num++; return true ; } bool insertLast (int value) { if (num == maxNum) { return false ; } if (num == 0 ) { rear = new DqNode (value, nullptr , nullptr ); front = rear; } else { rear->next = new DqNode (value, nullptr , rear); rear = rear->next; } this ->num++; return true ; } bool deleteFront () { if (num == 0 ) { return false ; } if (num == 1 ) { delete front; front = nullptr ; rear = nullptr ; } else { auto tmp = front; front = front->next; delete tmp; front->before = nullptr ; } this ->num--; return true ; } bool deleteLast () { if (num == 0 ) { return false ; } if (num == 1 ) { delete rear; front = nullptr ; rear = nullptr ; } else { auto tmp = rear; rear = rear->before; delete tmp; rear->next = nullptr ; } this ->num--; return true ; } int getFront () { return front ? front->val : -1 ; } int getRear () { return rear ? rear->val : -1 ; } bool isEmpty () { return this ->num == 0 ; } bool isFull () { return this ->num == this ->maxNum; } };
[1302] 层数最深叶子节点的和 Time: 2020-08-17
Difficulty: Medium (82.57%)
Tags: Unknown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 class Solution {public : int deepestLeavesSum (TreeNode *root) { std::queue<TreeNode *> bfs; bfs.push (root); for (; !bfs.empty ();) { auto num = bfs.size (); int ans = 0 ; for (; num--;) { auto fr = bfs.front (); bfs.pop (); ans += fr->val; if (fr->left) { bfs.push (fr->left); } if (fr->right) { bfs.push (fr->right); } } if (bfs.size () == 0 ) { return ans; } } return 0 ; } };
[1656] 设计有序流 Time: 2020-08-17
Difficulty: Easy (77.94%)
Tags: Unknown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 class OrderedStream {private : int num; int ptr = 0 ; vector<string> container; public : OrderedStream (int n) { this ->num = n; vector <string>(n).swap (container); } vector<string> insert (int idKey, string value) { idKey--; container[idKey] = value; if (ptr == idKey) { vector<string> ans; for (; ptr < this ->num && !container[ptr].empty ();) { ans.push_back (container[ptr]); ptr++; } return ans; } else { return {}; } } };