diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index 558b32b9..035d0e7e 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -76,11 +76,9 @@ public: for (int i = 0; i < nums.size(); i++) { nextDistance = max(nums[i] + i, nextDistance); // 更新下一步覆盖最远距离下标 if (i == curDistance) { // 遇到当前覆盖最远距离下标 - if (curDistance < nums.size() - 1) { // 如果当前覆盖最远距离下标不是终点 - ans++; // 需要走下一步 - curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了) - if (nextDistance >= nums.size() - 1) break; // 下一步的覆盖范围已经可以达到终点,结束循环 - } else break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束 + ans++; // 需要走下一步 + curDistance = nextDistance; // 更新当前覆盖最远距离下标(相当于加油了) + if (nextDistance >= nums.size() - 1) break; // 当前覆盖最远距到达集合终点,不用做ans++操作了,直接结束 } } return ans; diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 72dc69c5..952d32af 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -312,24 +312,22 @@ object Solution { ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } - } - - pub fn merge(intervals: Vec>) -> Vec> { - let mut intervals = intervals; - let mut result = Vec::new(); - if intervals.len() == 0 { return result; } - intervals.sort_by(|a, b| a[0].cmp(&b[0])); - result.push(intervals[0].clone()); - for i in 1..intervals.len() { - if result.last_mut().unwrap()[1] >= intervals[i][0] { - result.last_mut().unwrap()[1] = Self::max(result.last_mut().unwrap()[1], intervals[i][1]); + pub fn merge(mut intervals: Vec>) -> Vec> { + let mut res = vec![]; + if intervals.is_empty() { + return res; + } + intervals.sort_by_key(|a| a[0]); + res.push(intervals[0].clone()); + for interval in intervals.into_iter().skip(1) { + let res_last_ele = res.last_mut().unwrap(); + if res_last_ele[1] >= interval[0] { + res_last_ele[1] = interval[1].max(res_last_ele[1]); } else { - result.push(intervals[i].clone()); + res.push(interval); } } - result + res } } ``` diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 41c2e616..8f8bc9a6 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -101,6 +101,11 @@ public: }; ``` +* 时间复杂度: O(nm) +* 空间复杂度: O(n) + + + 代码中m表示最多可以爬m个台阶,代码中把m改成2就是本题70.爬楼梯可以AC的代码了。 ## 总结 diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 0b49e3c6..cc4ab00c 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -218,6 +218,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index b95e39c8..36578fd3 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -50,7 +50,7 @@ 代码如下: ```CPP -int getdepth(treenode* node) +int getdepth(TreeNode* node) ``` 2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。 @@ -76,14 +76,14 @@ return depth; ```CPP class solution { public: - int getdepth(treenode* node) { + int getdepth(TreeNode* node) { if (node == NULL) return 0; int leftdepth = getdepth(node->left); // 左 int rightdepth = getdepth(node->right); // 右 int depth = 1 + max(leftdepth, rightdepth); // 中 return depth; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { return getdepth(root); } }; @@ -93,9 +93,9 @@ public: ```CPP class solution { public: - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { if (root == null) return 0; - return 1 + max(maxdepth(root->left), maxdepth(root->right)); + return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; @@ -110,7 +110,7 @@ public: class solution { public: int result; - void getdepth(treenode* node, int depth) { + void getdepth(TreeNode* node, int depth) { result = depth > result ? depth : result; // 中 if (node->left == NULL && node->right == NULL) return ; @@ -127,7 +127,7 @@ public: } return ; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { result = 0; if (root == NULL) return result; getdepth(root, 1); @@ -144,7 +144,7 @@ public: class solution { public: int result; - void getdepth(treenode* node, int depth) { + void getdepth(TreeNode* node, int depth) { result = depth > result ? depth : result; // 中 if (node->left == NULL && node->right == NULL) return ; if (node->left) { // 左 @@ -155,7 +155,7 @@ public: } return ; } - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { result = 0; if (root == 0) return result; getdepth(root, 1); @@ -182,16 +182,16 @@ c++代码如下: ```CPP class solution { public: - int maxdepth(treenode* root) { + int maxDepth(TreeNode* root) { if (root == NULL) return 0; int depth = 0; - queue que; + queue que; que.push(root); while(!que.empty()) { int size = que.size(); depth++; // 记录深度 for (int i = 0; i < size; i++) { - treenode* node = que.front(); + TreeNode* node = que.front(); que.pop(); if (node->left) que.push(node->left); if (node->right) que.push(node->right); @@ -230,11 +230,11 @@ c++代码: ```CPP class solution { public: - int maxdepth(node* root) { + int maxDepth(Node* root) { if (root == 0) return 0; int depth = 0; for (int i = 0; i < root->children.size(); i++) { - depth = max (depth, maxdepth(root->children[i])); + depth = max (depth, maxDepth(root->children[i])); } return depth + 1; } @@ -247,15 +247,15 @@ public: ```CPP class solution { public: - int maxdepth(node* root) { - queue que; + int maxDepth(Node* root) { + queue que; if (root != NULL) que.push(root); int depth = 0; while (!que.empty()) { int size = que.size(); depth++; // 记录深度 for (int i = 0; i < size; i++) { - node* node = que.front(); + Node* node = que.front(); que.pop(); for (int j = 0; j < node->children.size(); j++) { if (node->children[j]) que.push(node->children[j]); diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 9b6027d1..5958de93 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -250,7 +250,7 @@ private: vector> result; vector path; // 递归函数不需要返回值,因为我们要遍历整个树 - void traversal(treenode* cur, int count) { + void traversal(TreeNode* cur, int count) { if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径 result.push_back(path); return; @@ -276,10 +276,10 @@ private: } public: - vector> pathsum(treenode* root, int sum) { + vector> pathSum(TreeNode* root, int sum) { result.clear(); path.clear(); - if (root == null) return result; + if (root == NULL) return result; path.push_back(root->val); // 把根节点放进路径 traversal(root, sum - root->val); return result; diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index daa708bc..6127f190 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -149,6 +149,11 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + + ## 其他语言版本 diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 1b006665..8fa7c77c 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -970,6 +970,49 @@ pub fn remove_extra_spaces(s: &mut Vec) { } } ``` +C: + +```C +// 翻转字符串中指定范围的字符 +void reverse(char* s, int start, int end) { + for (int i = start, j = end; i < j; i++, j--) { + int tmp = s[i]; + s[i] = s[j]; + s[j] = tmp; + } +} + +// 删除字符串两端和中间多余的空格 +void removeExtraSpace(char* s) { + int start = 0; // 指向字符串开头的指针 + int end = strlen(s) - 1; // 指向字符串结尾的指针 + while (s[start] == ' ') start++; // 移动指针 start,直到找到第一个非空格字符 + while (s[end] == ' ') end--; // 移动指针 end,直到找到第一个非空格字符 + int slow = 0; // 指向新字符串的下一个写入位置的指针 + for (int i = start; i <= end; i++) { // 遍历整个字符串 + if (s[i] == ' ' && s[i+1] == ' ') { // 如果当前字符是空格,并且下一个字符也是空格,则跳过 + continue; + } + s[slow] = s[i]; // 否则,将当前字符复制到新字符串的 slow 位置 + slow++; // 将 slow 指针向后移动 + } + s[slow] = '\0'; // 在新字符串的末尾添加一个空字符 +} + +// 翻转字符串中的单词 +char * reverseWords(char * s){ + removeExtraSpace(s); // 先删除字符串两端和中间的多余空格 + reverse(s, 0, strlen(s) - 1); // 翻转整个字符串 + int slow = 0; // 指向每个单词的开头位置的指针 + for (int i = 0; i <= strlen(s); i++) { // 遍历整个字符串 + if (s[i] ==' ' || s[i] == '\0') { // 如果当前字符是空格或空字符,说明一个单词结束了 + reverse(s, slow, i-1); // 翻转单词 + slow = i + 1; // 将 slow 指针指向下一个单词的开头位置 + } + } + return s; // 返回处理后的字符串 +} +```

diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index 4fdd7bf4..f6744a2b 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(n * k),其中 n 为 prices 的长度 +* 空间复杂度: O(n * k) + + + 当然有的解法是定义一个三维数组dp[i][j][k],第i天,第j次买卖,k表示买还是卖的状态,从定义上来讲是比较直观。 但感觉三维数组操作起来有些麻烦,我是直接用二维数组来模拟三维数组的情况,代码看起来也清爽一些。 diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index fdb2dabf..c25f3b86 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -108,6 +108,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 总结 打家劫舍是DP解决的经典题目,这道题也是打家劫舍入门级题目,后面我们还会变种方式来打劫的。 diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 7e3955a5..b52f16ea 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -316,8 +316,8 @@ class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: dummy_head = ListNode(next=head) #添加一个虚拟节点 cur = dummy_head - while(cur.next!=None): - if(cur.next.val == val): + while cur.next: + if cur.next.val == val: cur.next = cur.next.next #删除cur.next节点 else: cur = cur.next diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 0627eedb..becad069 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -82,6 +82,11 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + + ## 总结 成环之后还是难了一些的, 不少题解没有把“考虑房间”和“偷房间”说清楚。 diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index c329156b..f5b23d26 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -127,6 +127,10 @@ public: }; ``` +* 时间复杂度: O(n * √n) +* 空间复杂度: O(n) + + 同样我在给出先遍历物品,在遍历背包的代码,一样的可以AC的。 ```CPP @@ -145,6 +149,8 @@ public: } }; ``` +* 同上 + ## 总结 diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 478837cc..e8cb0b5f 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -106,6 +106,10 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n) + + ## 总结 diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 3be72565..0e3947da 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -133,6 +133,11 @@ public: }; ``` +* 时间复杂度: O(n * amount),其中 n 为 coins 的长度 +* 空间复杂度: O(amount) + + + 对于遍历方式遍历背包放在外循环,遍历物品放在内循环也是可以的,我就直接给出代码了 ```CPP @@ -154,6 +159,8 @@ public: } }; ``` +* 同上 + ## 总结 diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 9bd5df7a..95e7d3ed 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -380,7 +380,33 @@ class Solution: backtracking("JFK") return path ``` + +python - 使用used数组 - 神似之前几题写法 +```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + global used,path,results + used = [0]*len(tickets) + path = ['JFK'] + results = [] + tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的 + self.backtracking(tickets,'JFK') + return results[0] + def backtracking(self,tickets,cur): + if sum(used) == len(tickets): + results.append(path[:]) + return True # 只要找到就返回 + for i in range(len(tickets)): + if tickets[i][0]==cur and used[i]==0: + used[i]=1 + path.append(tickets[i][1]) + state = self.backtracking(tickets,tickets[i][1]) + path.pop() + used[i]=0 + if state: return True # 只要找到就返回,不继续搜索了 +``` + ### GO ```go type pair struct { diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index f2287188..ee659723 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -130,6 +130,11 @@ public: ``` +* 时间复杂度: O(target * n),其中 n 为 nums 的长度 +* 空间复杂度: O(target) + + + C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 但java就不用考虑这个限制,java里的int也是四个字节吧,也有可能leetcode后台对不同语言的测试数据不一样。 diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 8d79d701..6a178a25 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -156,6 +156,11 @@ public: }; ``` +* 时间复杂度: O(kmn),k 为strs的长度 +* 空间复杂度: O(mn) + + + ## 总结 不少同学刷过这道题,可能没有总结这究竟是什么背包。 diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index d28a33cc..fcdd57b0 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -144,6 +144,11 @@ public: } }; ``` +* 时间复杂度: O(n^2) +* 空间复杂度: O(n^2) + + + ## 其他语言版本 diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index eb5a844c..c208754f 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -179,6 +179,11 @@ public: } }; ``` + +* 时间复杂度: O(mn),其中 m 是amount,n 是 coins 的长度 +* 空间复杂度: O(m) + + 是不是发现代码如此精简,哈哈 ## 总结 diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 29b21840..ad5310e1 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -234,6 +234,26 @@ class Solution: return root ``` +**迭代** +```python +class Solution: + def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if not root: return root + stack = [] + result = [] + cur = root + pre = 0 + while cur or stack: + if cur: + stack.append(cur) + cur = cur.right + else: + cur = stack.pop() + cur.val+= pre + pre = cur.val + cur =cur.left + return root +``` ## Go diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index eb046a9d..561ad2f2 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -104,6 +104,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ### 动态规划二 @@ -127,6 +131,10 @@ public: }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 其他语言版本 diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index f92652cd..1cf8a0a6 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -280,18 +280,20 @@ object Solution { ```Rust impl Solution { pub fn monotone_increasing_digits(n: i32) -> i32 { - let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::>(); - let mut flag = str_num.len(); - for i in (1..str_num.len()).rev() { - if str_num[i - 1] > str_num[i] { + let mut n_bytes = n.to_string().into_bytes(); + let mut flag = n_bytes.len(); + for i in (1..n_bytes.len()).rev() { + if n_bytes[i - 1] > n_bytes[i] { flag = i; - str_num[i - 1] -= 1; + n_bytes[i - 1] -= 1; } } - for i in flag..str_num.len() { - str_num[i] = 9; + for v in n_bytes.iter_mut().skip(flag) { + *v = 57; } - str_num.iter().fold(0, |acc, x| acc * 10 + x) + n_bytes + .into_iter() + .fold(0, |acc, x| acc * 10 + x as i32 - 48) } } ``` diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 263a28f4..749dc972 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -271,6 +271,7 @@ class Solution { ``` Python: +> 未精简版本 ```python class Solution: @@ -291,6 +292,21 @@ class Solution: return answer ``` +> 精简版本 + +```python +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + answer = [0]*len(temperatures) + stack = [] + for i in range(len(temperatures)): + while len(stack)>0 and temperatures[i] > temperatures[stack[-1]]: + answer[stack[-1]] = i - stack[-1] + stack.pop() + stack.append(i) + return answer +``` + Go: > 暴力法 diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 6534f18c..ac56f6f9 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -535,8 +535,57 @@ function getIndexAfterDel(s: string, startIndex: number): number { } ``` +### Rust +> 双指针 +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + let (s, t) = ( + s.chars().collect::>(), + t.chars().collect::>(), + ); + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(mut chars: Vec) -> Vec { + let mut slow = 0; + for i in 0..chars.len() { + if chars[i] == '#' { + slow = (slow as u32).saturating_sub(1) as usize; + } else { + chars[slow] = chars[i]; + slow += 1; + } + } + chars.truncate(slow); + chars + } +} +``` + +> 双栈法 + +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(string: String) -> String { + let mut s = String::new(); + for c in string.chars() { + if c != '#' { + s.push(c); + } else if !s.is_empty() { + s.pop(); + } + } + s + } +} +```

diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index d1e1e30a..7b60abdd 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -62,6 +62,10 @@ public: } }; ``` +* 时间复杂度: O(n * m) +* 空间复杂度: O(n * m) + + ## 总结 diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index 8e5f7eb2..730e9ad1 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -124,6 +124,10 @@ public: } }; ``` +* 时间复杂度: O(n * m),其中 n 和 m 分别为 text1 和 text2 的长度 +* 空间复杂度: O(n * m) + + ## 其他语言版本