# [朝花夕拾] LeetCode刷题记录

20220228

1. 两数之和

 1 class Solution {
2 public:
3     vector<int> twoSum(vector<int>& nums, int target) {
4         int n = nums.size();
5         for (int i = 0; i < n - 1; i++)
6             for (int j = i + 1; j < n; j++)
7                 if (nums[i] + nums[j] == target)
8                     return {i, j};
9         return {};
10     }
11 };

 1 class Solution {
2 public:
3     vector<int> twoSum(vector<int>& nums, int t) {
4         int n = nums.size();
5         map<int, int> mp;
6         for (int i = 0; i < n; i++) {
7             auto it = mp.find(t - nums[i]);
8             if (it != mp.end())
9                 return {it -> second, i};
10             mp[nums[i]] = i;
11         }
12         return {};
13     }
14 };

2. 两数相加

 1 class Solution {
2 public:
3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
4         int c = 0;
5         ListNode *h = NULL, *t = NULL;
6         while (l1 || l2) {
7             int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + c;
8             if (!h)
9                 h = t = new ListNode(sum % 10);
10             else {
11                 t->next = new ListNode(sum % 10);
12                 t = t->next;
13             }
14             c = sum / 10;
15             if (l1)
16                 l1 = l1->next;
17             if (l2)
18                 l2 = l2->next;
19         }
20         if (c > 0)
21             t->next = new ListNode(c);
22         return h;
23     }
24 };

3. 无重复字符的最长子串

（官方的题解的左指针是依次向右移动而非跳跃的，大概是因为使用的 set，这样相当于用更多时间换取了更少空间）

 1 const int N = 130;
2
3 class Solution {
4 public:
5     int lengthOfLongestSubstring(string s) {
6         int l = s.length(), h = 0, t = 0, ans = 0, a[N];
7         memset(a, -1, sizeof(a));
8         while (t < l) {
9             if (a[s[t]] != -1)
10             　　h = max(h, a[s[t]] + 1);
11             a[s[t]] = t;
12             ans = max(ans, t - h + 1);
13             t++;
14         }
15         return ans;
16     }
17 };

20220301

4. 不会做

5. 最长回文子串

 1 class Solution {
2 public:
3     string longestPalindrome(string s) {
4         int l = s.length(), ans = 1, st = 0;
5         for (int i = 0; i < l; i++) {
6             int j = 0;
7             while (j <= i && i + j < l && s[i - j] == s[i + j])
8                 j++;
9             if (2 * j - 1 > ans)
10                 st = i - j + 1, ans = 2 * j - 1;
11         }
12         for (int i = 0; i < l - 1; i++) {
13             if (s[i] != s[i + 1]) continue;
14             int j = 1;
15             while (j <= i && i + j - 1 < l && s[i - j] == s[i + j + 1])
16                 j++;
17             if (2 * j > ans)
18                 st = i - j + 1, ans = 2 * j;
19         }
20         return s.substr(st, ans);
21     }
22 };

P.S. string 的截取函数为 substr。

7. 整数反转

1 class Solution {
2 public:
3     int reverse(int x) {
4         long long o = 0;
5         while (x) o *= 10, o += x % 10, x /= 10;
6         if (o < INT_MIN || o > INT_MAX) return 0;
7         return o;
8     }
9 };

8. 字符串转换整数（atoi）

 1 class Solution {
2 public:
3     int myAtoi(string s) {
4         int o = 0, l = s.length(), f = 0;
5         long long ans = 0;
6         while (o < l && s[o] == ' ') o++;
7         if (o < l && s[o] == '+') f = 0, o++;
8         else if (o < l && s[o] == '-') f = 1, o++;
9         while (o < l && s[o] == '0') o++;
10         while (o < l && s[o] >= '0' && s[o] <= '9') {
11             ans *= 10, ans += s[o] - '0', o++;
12             if (ans > INT_MAX) return f ? INT_MIN : INT_MAX;
13         }
14         if (f) ans = -ans;
15         return ans;
16     }
17 };

20220302

 1 class Solution {
2 public:
3     int findRepeatNumber(vector<int>& a) {
4         int l = a.size();
5         sort(a.begin(), a.end());
6         for (int i = 1; i < l; i++)
7             if (a[i] == a[i - 1])
8                 return a[i];
9         return 0;
10     }
11 };

 1 class Solution {
2 public:
3     int findRepeatNumber(vector<int>& a) {
4         int l = a.size();
5         set <int> s;
6         for (int i = 0; i < l; i++) {
7             if (s.count(a[i])) return a[i];
8             s.insert(a[i]);
9         }
10         return 0;
11     }
12 };

class Solution {
public:
int findRepeatNumber(vector<int>& a) {
map <int, bool> mp;
for (int i : a) {
if (mp[i]) return i;
mp[i] = 1;
}
return 0;
}
};

 1 class Solution {
2 public:
3     int findRepeatNumber(vector<int>& a) {
4         int l = a.size();
5         for (int i = 0; i < l; i++)
6             while (i != a[i]) {
7                 if (a[a[i]] == a[i]) return a[i];
8                 swap(a[i], a[a[i]]);
9             }
10         return 0;
11     }
12 };

 1 class Solution {
2 public:
3     bool dfs(int x, int y, vector<vector<int>>& a, int target) {
4         if (x >= a.size() || y >= a[0].size() || x < 0 || y < 0 || f[x][y]) return 0;
5         f[x][y] = 1;
6         if (a[x][y] == target) return 1;
7         if (a[x][y] < target)
8             return dfs(x + 1, y, a, target) || dfs(x, y + 1, a, target);
9         else
10             return dfs(x - 1, y, a, target) || dfs(x, y - 1, a, target);
11     }
12     bool findNumberIn2DArray(vector<vector<int>>& a, int target) {
13         return dfs(0, 0, a, target);
14     }
15 };

class Solution {
public:
bool findNumberIn2DArray(vector<vector<int>>& a, int target) {
if (!a.size() || !a[0].size()) return 0;
int x = 0, y = a[0].size() - 1;
while (x < a.size() && y >= 0) {
if (a[x][y] == target) return 1;
if (a[x][y] < target) x++;
else y--;
}
return 0;
}
};

20220303

 1 class Solution {
2 public:
3     string replaceSpace(string s) {
4         int l = s.length();
5         string ans;
6         for (auto& c : s)
7             if (s == ' ') ans += "%20";
8             else ans += s;
9         return ans;
10     }
11 };

 1 class Solution {
2 public:
4         stack<int> s;
5         vector<int> a;
7         while (p)
8             s.push(p->val), p = p->next;
9         while (!s.empty())
10             a.push_back(s.top()), s.pop();
11         return a;
12     }
13 };

 1 class Solution {
2 private:
3     map<int, int> ind;
4 public:
5     TreeNode* build(vector<int>& p, vector<int>& i, int pl, int pr, int il, int ir) {
6         if (pl > pr) return nullptr;
7         int root_p = pl, root_i = ind[p[pl]];
8         TreeNode* root = new TreeNode(p[pl]);
9         int size = root_i - il;
10         root->left = build(p, i, pl + 1, pl + size, il, root_i - 1);
11         root->right = build(p, i, pl + size + 1, pr, root_i + 1, ir);
12         return root;
13     }
14
15     TreeNode* buildTree(vector<int>& p, vector<int>& i) {
16         int n = p.size();
17         for (int o = 0; o < n; o++)
18             ind[i[o]] = o;
19         return build(p, i, 0, n - 1, 0, n - 1);
20     }
21 };

20220304

 1 class CQueue {
2     stack<int> s1, s2;
3 public:
4     CQueue() {
5         while (!s1.empty())
6             s1.pop();
7         while (!s2.empty())
8             s2.pop();
9     }
10
11     void appendTail(int value) {
12         s1.push(value);
13     }
14
16         if (s2.empty())
17             while (!s1.empty())
18                 s2.push(s1.top()), s1.pop();
19         if (s2.empty())
20             return -1;
21         int res = s2.top();
22         s.pop();
23         return res;
24     }
25 };

 1 const int MOD = 1e9 + 7;
2
3 class Solution {
4 public:
5     int fib(int n) {
6         int a = 0, b = 1;
7         for (int i = 1; i <= n; i++)
8             ((i & 1) ? a : b) = (a + b) % MOD;
9         return n & 1 ? b : a;
10     }
11 };

20220305

1 class Solution {
2 public:
3     int minArray(vector<int>& numbers) {
4         int mi = INT_MAX;
5         for (int i : numbers)
6             mi = min(mi, i);
7         return mi;
8     }
9 };

 1 class Solution {
2 public:
3     int minArray(vector<int>& a) {
4         int n = a.size();
5         int l = 0, r = n - 1;
6         while (l < r) {
7             int m = (l + r) >> 1;
8             if (a[m] < a[r]) r = m;
9             else if (a[m] > a[r]) l = m + 1;
10             else r--;
11         }
12         return a[l];
13     }
14 };

 1 const int vx[4] = {0, 0, 1, -1}, vy[4] = {1, -1, 0, 0};
2
3 class Solution {
4 public:
5     bool dfs(vector<vector<char>>& b, vector<vector<char>>& f, int n, int m, string word, int l, int x, int y, int d) {
6         f[x][y] = 1;
7         if (d == l - 1)
8             return 1;
9         bool res = 0;
10         for (int i = 0; i < 4; i++) {
11             int tx = x + vx[i], ty = y + vy[i];
12             if (tx >= 0 && tx < n && ty >= 0 && ty < m && !f[tx][ty] && b[tx][ty] == word[d + 1])
13                 res = dfs(b, f, n, m, word, l, tx, ty, d + 1);
14             if (res) return 1;
15         }
16         f[x][y] = 0;
17         return 0;
18     }
19     bool exist(vector<vector<char>>& b, string word) {
20         int n = b.size(), m = b[0].size(), l = word.length();
21         vector<vector<char>> f(n, vector<char>(m));
22         for (int i = 0; i < n; i++)
23             for (int j = 0; j < m; j++)
24                 if (b[i][j] == word[0]) {
25                     for (int k = 0; k < n; k++)
26                         f[k].assign(m, 0);
27                     if (dfs(b, f, n, m, word, l, i, j, 0)) return 1;
28                 }
29         return 0;
30     }
31 };

20220307 剑指 Offer 13. 机器人的运动范围

 1 const int vx[4] = {0, 0, 1, -1}, vy[4] = {1, -1, 0, 0};
2 const int N = 1e2 + 5;
3
4 class Solution {
5     int ans = 0, f[N][N];
6 public:
7     int calc(int o) {
8         return o < 10 ? o : o == 100 ? 1 : o % 10 + o / 10;
9     }
10     void dfs(int x, int y, int m, int n, int k) {
11         f[x][y] = 1, ans++;
12         for (int i = 0; i < 4; i++) {
13             int tx = x + vx[i], ty = y + vy[i];
14             if (tx >= 0 && ty >= 0 && tx < m && ty < n && !f[tx][ty] && calc(tx) + calc(ty) <= k)
15                 dfs(tx, ty, m, n, k);
16         }
17     }
18     int movingCount(int m, int n, int k) {
19         dfs(0, 0, m, n, k);
20         return ans;
21     }
22 };

20220310 剑指 Offer 14. 剪绳子

1 class Solution {
2 public:
3     int cuttingRope(int n) {
4         if (n <= 3) return n - 1;
5         int a = n / 3, b = n % 3;
6         return !b ? pow(3, a) : b == 1 ? pow(3, a - 1) * 4 : pow(3, a) * 2;
7     }
8 };

 1 const int MOD = 1e9 + 7;
2
3 class Solution {
4 public:
5     long long mypow(int a, int b) {
6         long long o = a, res = 1;
7         while (b) {
8             if (b & 1) (res *= o) %= MOD;
9             (o *= o) %= MOD;
10             b >>= 1;
11         }
12         return res;
13     }
14     int cuttingRope(int n) {
15         if (n <= 3) return n - 1;
16         int a = n / 3, b = n % 3;
17         return !b ? mypow(3, a) : b == 1 ? (mypow(3, a - 1) * 4) % MOD: (mypow(3, a) * 2) % MOD;
18     }
19 };

20220329 剑指 Offer 63. 股票的最大利润

 1 class Solution {
2 public:
3     int maxProfit(vector<int>& a) {
4         int n = a.size();
5         if (!n) return 0;
6         int mi = a[0], ans = 0;
7         for (int i = 1; i < n; i++)
8             if (a[i] < mi) mi = a[i];
9             else ans = max(ans, a[i] - mi);
10         return ans;
11     }
12 };

122. 买卖股票的最佳时机 II

1 class Solution {
2 public:
3     int maxProfit(vector<int>& a) {
4         int n = a.size(), ans= 0;
5         for (int i = 1; i < n; i++)
6             ans += a[i] > a[i - 1] ? a[i] - a[i - 1];
7         return ans;
8     }
9 };

123. 买卖股票的最佳时机 III

 1 class Solution {
2 public:
3     int maxProfit(vector<int>& a) {
4         int n = a.size();
5         int b1 = -a[0], s1 = 0, b2 = -a[0], s2 = 0;
6         for (int i = 1; i < n; i++) {
7             b1 = max(b1, -a[i]), s1 = max(s1, b1 + a[i]);
8             b2 = max(b2, s1 - a[i]), s2 = max(s2, b2 + a[i]);
9         }
10         return s2;
11     }
12 };

124. 买卖股票的最佳时机 III

 1 typedef vector<int> arr;
2 typedef vector<arr> arr2;
3
4 class Solution {
5 public:
6     int maxProfit(int k, arr& a) {
7         if (a.empty()) return 0;
8         int n = a.size();
9         k = min(k, n / 2);
10         arr2 b(n, arr(k + 1));
11         arr2 s(n, arr(k + 1));
12         b[0][0] = -a[0], s[0][0] = 0;
13         for (int i = 1; i <= k; ++i)
14             b[0][i] = s[0][i] = INT_MIN / 2;
15         for (int i = 1; i < n; ++i) {
16             b[i][0] = max(b[i - 1][0], s[i - 1][0] - a[i]);
17             for (int j = 1; j <= k; ++j) {
18                 b[i][j] = max(b[i - 1][j], s[i - 1][j] - a[i]);
19                 s[i][j] = max(s[i - 1][j], b[i - 1][j - 1] + a[i]);
20             }
21         }
22         return *max_element(s[n - 1].begin(), s[n - 1].end());
23     }
24 };

20220409

144. 二叉树的前序遍历

 1 /**
2  * Definition for a binary tree node.
3  * struct TreeNode {
4  *     int val;
5  *     TreeNode *left;
6  *     TreeNode *right;
7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     vector<int> res;
15     vector<int> preorderTraversal(TreeNode* root) {
16         if (root == nullptr)
17             return res;
18         res.emplace_back(root->val);
19         preorderTraversal(root->left);
20         preorderTraversal(root->right);
21         return res;
22     }
23 };

 1 class Solution {
2 public:
3     vector<int> res;
4     vector<int> preorderTraversal(TreeNode* root) {
5         stack<TreeNode*> st;
6         TreeNode* node = root;
7         while (!st.empty() || node) {
8             while (node) {
9                 st.push(node);
10                 res.emplace_back(node->val);
11                 node = node->left;
12             }
13             node = st.top();
14             st.pop();
15             node = node->right;
16         }
17         return res;
18     }
19 };

94. 二叉树的中序遍历

 1 class Solution {
2 public:
3     vector<int> res;
4     vector<int> inorderTraversal(TreeNode* root) {
5         if (root == nullptr)
6             return res;
7         inorderTraversal(root->left);
8         res.emplace_back(root->val);
9         inorderTraversal(root->right);
10         return res;
11     }
12 };

 1 class Solution {
2 public:
3     vector<int> res;
4     vector<int> inorderTraversal(TreeNode* root) {
5         stack<TreeNode*> st;
6         TreeNode* node = root;
7         while (!st.empty() || node) {
8             while (node) {
9                 st.push(node);
10                 node = node->left;
11             }
12             node = st.top();
13             res.emplace_back(node->val);
14             st.pop();
15             node = node->right;
16         }
17         return res;
18     }
19 };

145. 二叉树的后序遍历

 1 class Solution {
2 public:
3     vector<int> res;
4     vector<int> postorderTraversal(TreeNode* root) {
5         if (root == nullptr)
6             return res;
7         postorderTraversal(root->left);
8         postorderTraversal(root->right);
9         res.emplace_back(root->val);
10         return res;
11     }
12 };

 1 class Solution {
2 public:
3     vector<int> res;
4     vector<int> postorderTraversal(TreeNode* root) {
5         stack<TreeNode*> st1, st2;
6         if (!root) return res;
7         TreeNode* node = root;
8         st1.push(root);
9         while (!st1.empty()){
10             TreeNode* node = st1.top();
11             st1.pop();
12             st2.push(node);
13             if (node->left) st1.push(node->left);
14             if (node->right) st1.push(node->right);
15         }
16         while (!st2.empty()) {
17             res.push_back(st2.top()->val);
18             st2.pop();
19         }
20         return res;
21     }
22 };

20220412

 1 class Solution {
2 public:
3     bool cmp(TreeNode* A, TreeNode* B) {
4         if (B == nullptr) return true;
5         if (A == nullptr || A->val != B->val) return false;
6         return cmp(A->left, B->left) && cmp(A->right, B->right);
7     }
8     bool isSubStructure(TreeNode* A, TreeNode* B) {
9         if (A == nullptr || B == nullptr) return false;
10         if (A->val == B->val && cmp(A, B))
11             return true;
12         return isSubStructure(A->left, B) || isSubStructure(A->right, B);
13     }
14 };

20220416

 1 class Solution {
2     vector<int> ans;
3 public:
5         int l = s.length() - 1;
6         s[l]++;
7         while (l) {
8             if (s[l] > '9')
9                 s[l] = '0', s[l - 1]++;
10             l--;
11         }
12     }
13     void work(int o) {
14         string s = "1";
15         for (int i = 1; i < o; i++)
16             s += '0';
17         ans.push_back(stoi(s));
18         int x = 9 * (int)pow(10, o - 1);
19         for (int i = 1; i < x; i++)
21     }
22     vector<int> printNumbers(int n) {
23         for (int i = 1; i <= n; i++)
24             work(i);
25         return ans;
26     }
27 };

1944. 队列中可以看到的人

 1 class Solution {
2 public:
3     vector<int>canSeePersonsCount(vector<int>& a) {
4         int n = a.size();
5         vector<int> ans(n);
6         stack<int> s;
7         for (int i = n - 1; i >= 0; --i) {
8             while (!s.empty()) {
9                 ans[i]++;
10                 if (a[i] > a[s.top()]) s.pop();
11                 else break;
12             }
13             s.push(i);
14         }
15         return ans;
16     }
17 };

posted @ 2022-02-28 18:56  jinkun113  阅读(35)  评论(0编辑  收藏  举报