diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index b3abb991..eea3ba7a 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -216,6 +216,26 @@ impl Solution { } } ``` +Rust + +``` +use std::collections::HashMap; + +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut hm: HashMap = HashMap::new(); + for i in 0..nums.len() { + let j = target - nums[i]; + if hm.contains_key(&j) { + return vec![*hm.get(&j).unwrap(), i as i32] + } else { + hm.insert(nums[i], i as i32); + } + } + vec![-1, -1] + } +} +``` Javascript diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index a25129c5..213d61b7 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -135,6 +135,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + 技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。 diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 17d2cc0a..ec659108 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -177,6 +177,34 @@ func max(a, b int) int { return b } ``` +```go +// 版本2 +func merge(intervals [][]int) [][]int { + if len(intervals) == 1 { + return intervals + } + sort.Slice(intervals, func(i, j int) bool { + return intervals[i][0] < intervals[j][0] + }) + res := make([][]int, 0) + res = append(res, intervals[0]) + for i := 1; i < len(intervals); i++ { + if intervals[i][0] <= res[len(res)-1][1]{ + res[len(res)-1][1] = max56(res[len(res)-1][1],intervals[i][1]) + } else { + res = append(res, intervals[i]) + } + } + return res +} +func max56(a, b int) int { + if a > b { + return a + } + return b +} +``` + ### Javascript ```javascript diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index d8246223..a06ee91e 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -72,7 +72,7 @@ dp[i]: 爬到第i层楼梯,有dp[i]种方法 3. dp数组如何初始化 -再回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]中方法。 +再回顾一下dp[i]的定义:爬到第i层楼梯,有dp[i]种方法。 那么i为0,dp[i]应该是多少呢,这个可以有很多解释,但基本都是直接奔着答案去解释的。 diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index f0323bc4..09cc4f96 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -113,6 +113,9 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + ## 题外话 diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 16b9e47c..bad2faec 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -111,6 +111,8 @@ public: } }; ``` +* 时间复杂度: push为O(n),其他为O(1) +* 空间复杂度: O(n) # 优化 @@ -156,6 +158,9 @@ public: } }; ``` +* 时间复杂度: push为O(n),其他为O(1) +* 空间复杂度: O(n) + # 其他语言版本 diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index efeb1046..4a57ee96 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -112,6 +112,10 @@ public: ``` +* 时间复杂度: push和empty为O(1), pop和peek为O(n) +* 空间复杂度: O(n) + + ## 拓展 可以看出peek()的实现,直接复用了pop(), 要不然,对stOut判空的逻辑又要重写一遍。 diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 540ab5d7..f1c4b76c 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -184,6 +184,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(k) + 再来看一下时间复杂度,使用单调队列的时间复杂度是 O(n)。 diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index d8eabef4..0d268d9b 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -79,8 +79,6 @@ ```CPP -// 时间复杂度:O(nlogk) -// 空间复杂度:O(n) class Solution { public: // 小顶堆 @@ -120,6 +118,10 @@ public: } }; ``` + +* 时间复杂度: O(nlogk) +* 空间复杂度: O(n) + # 拓展 大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。 diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index c2a5710f..411b60e8 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -97,26 +97,25 @@ Java: ```Java class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { - Map map = new HashMap<>(); - int temp; int res = 0; + Map map = new HashMap(); //统计两个数组中的元素之和,同时统计出现的次数,放入map for (int i : nums1) { for (int j : nums2) { - temp = i + j; - if (map.containsKey(temp)) { - map.put(temp, map.get(temp) + 1); + int tmp = map.getOrDefault(i + j, 0); + if (tmp == 0) { + map.put(i + j, 1); } else { - map.put(temp, 1); + map.replace(i + j, tmp + 1); } } } //统计剩余的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数 for (int i : nums3) { for (int j : nums4) { - temp = i + j; - if (map.containsKey(0 - temp)) { - res += map.get(0 - temp); + int tmp = map.getOrDefault(0 - i - j, 0); + if (tmp != 0) { + res += tmp; } } } diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 3fd03e16..eb5a844c 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -215,6 +215,27 @@ class Solution { } } ``` +```Java +// 二维dp数组版本,方便理解 +class Solution { + public int change(int amount, int[] coins) { + int[][] dp = new int[coins.length][amount + 1]; + // 只有一种硬币的情况 + for (int i = 0; i <= amount; i += coins[0]) { + dp[0][i] = 1; + } + for (int i = 1; i < coins.length; i++) { + for (int j = 0; j <= amount; j++) { + // 第i种硬币使用0~k次,求和 + for (int k = 0; k * coins[i] <= j; k++) { + dp[i][j] += dp[i - 1][j - k * coins[i]]; + } + } + } + return dp[coins.length - 1][amount]; + } +} +``` Python: diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index d6b3a177..d7258d45 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -92,7 +92,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。 这里就要说明本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。 -新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 从 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。 +新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。 所以初始化 dp[0] = 0,dp[1] = 0; diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 810f7292..694f1a92 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -77,6 +77,9 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) + 当然可以拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。 @@ -99,6 +102,8 @@ public: } }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(1),返回值不计空间复杂度 ## 题外话 diff --git a/problems/根据身高重建队列(vector原理讲解).md b/problems/根据身高重建队列(vector原理讲解).md index 94d94e38..4f8cab82 100644 --- a/problems/根据身高重建队列(vector原理讲解).md +++ b/problems/根据身高重建队列(vector原理讲解).md @@ -177,6 +177,35 @@ Java: Python: +Rust: + +```rust +// 版本二,使用list(链表) +use std::collections::LinkedList; +impl Solution{ + pub fn reconstruct_queue(mut people: Vec>) -> Vec> { + let mut queue = LinkedList::new(); + people.sort_by(|a, b| { + if a[0] == b[0] { + return a[1].cmp(&b[1]); + } + b[0].cmp(&a[0]) + }); + queue.push_back(people[0].clone()); + for v in people.iter().skip(1) { + if queue.len() > v[1] as usize { + let mut back_link = queue.split_off(v[1] as usize); + queue.push_back(v.clone()); + queue.append(&mut back_link); + } else { + queue.push_back(v.clone()); + } + } + queue.into_iter().collect() + } +} +``` + Go: