diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 6fce4c6e..e3b75719 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -214,19 +214,17 @@ end ``` Rust: ```rust -pub fn remove_element(nums: &mut Vec, val: i32) -> &mut Vec { - let mut start: usize = 0; - while start < nums.len() { - if nums[start] == val { - nums.remove(start); +impl Solution { + pub fn remove_element(nums: &mut Vec, val: i32) -> i32 { + let mut slowIdx = 0; + for pos in (0..nums.len()) { + if nums[pos]!=val { + nums[slowIdx] = nums[pos]; + slowIdx += 1; + } } - start += 1; + return (slowIdx) as i32; } - nums -} -fn main() { - let mut nums = vec![5,1,3,5,2,3,4,1]; - println!("{:?}",remove_element(&mut nums, 5)); } ``` diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index ff1b6d4a..29aa5b83 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -139,17 +139,11 @@ Java: // 贪心思路 class Solution { public int maxProfit(int[] prices) { - int sum = 0; - int profit = 0; - int buy = prices[0]; + int result = 0; for (int i = 1; i < prices.length; i++) { - profit = prices[i] - buy; - if (profit > 0) { - sum += profit; - } - buy = prices[i]; + result += Math.max(prices[i] - prices[i - 1], 0); } - return sum; + return result; } } ``` diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 45d2a229..ceca8c87 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -237,6 +237,32 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int { } ``` +Rust: + +```rust +impl Solution { + pub fn min_sub_array_len(target: i32, nums: Vec) -> i32 { + let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0); + let (mut sum, mut i) = (0, 0); + + for (pos, val) in nums.iter().enumerate() { + sum += val; + while sum >= target { + subLength = (pos - i + 1) as i32; + if result > subLength { + result = subLength; + } + sum -= nums[i]; + i += 1; + } + } + if result == i32::MAX { + return 0; + } + result + } +} +``` ----------------------- diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index b6fa4a98..1e1c4afe 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -117,6 +117,7 @@ public: Java: ```java class Solution { + // 思路1:优先考虑饼干,小饼干先喂饱小胃口 public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); @@ -132,10 +133,30 @@ class Solution { } } ``` +```java +class Solution { + // 思路2:优先考虑胃口,先喂饱大胃口 + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int count = 0; + int start = s.length - 1; + // 遍历胃口 + for (int index = g.length - 1; index >= 0; index--) { + if(start >= 0 && g[index] <= s[start]) { + start--; + count++; + } + } + return count; + } +} +``` Python: ```python3 class Solution: + # 思路1:优先考虑胃饼干 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -145,6 +166,20 @@ class Solution: res += 1 return res ``` +```python +class Solution: + # 思路2:优先考虑胃口 + def findContentChildren(self, g: List[int], s: List[int]) -> int: + g.sort() + s.sort() + start, count = len(s) - 1, 0 + for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口 + if start >= 0 and g[index] <= s[start]: + start -= 1 + count += 1 + return count +``` + Go: ```golang //排序后,局部最优 diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index bdcb0ff8..50d5795f 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -152,7 +152,35 @@ class Solution { } } ``` +```java +// 解法3 +class Solution { + public String reverseStr(String s, int k) { + char[] ch = s.toCharArray(); + // 1. 每隔 2k 个字符的前 k 个字符进行反转 + for (int i = 0; i< ch.length; i += 2 * k) { + // 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符 + if (i + k <= ch.length) { + reverse(ch, i, i + k -1); + continue; + } + // 3. 剩余字符少于 k 个,则将剩余字符全部反转 + reverse(ch, i, ch.length - 1); + } + return new String(ch); + } + // 定义翻转函数 + public void reverse(char[] ch, int i, int j) { + for (; i < j; i++, j--) { + char temp = ch[i]; + ch[i] = ch[j]; + ch[j] = temp; + } + + } +} +``` Python: ```python class Solution: diff --git a/problems/前序/上海互联网公司总结.md b/problems/前序/上海互联网公司总结.md index 05cbe1b0..08c15895 100644 --- a/problems/前序/上海互联网公司总结.md +++ b/problems/前序/上海互联网公司总结.md @@ -112,7 +112,7 @@ ## 总结 -大家如果看了[北京有这些互联网公司,你都知道么?](https://mp.weixin.qq.com/s/BKrjK4myNB-FYbMqW9f3yw)和[深圳原来有这么多互联网公司,你都知道么?](https://mp.weixin.qq.com/s/3VJHF2zNohBwDBxARFIn-Q)就可以看出中国互联网氛围最浓的当然是北京,其次就是上海! +大家如果看了[北京有这些互联网公司,你都知道么?](https://programmercarl.com/前序/北京互联网公司总结.html)和[深圳原来有这么多互联网公司,你都知道么?](https://programmercarl.com/前序/深圳互联网公司总结.html)就可以看出中国互联网氛围最浓的当然是北京,其次就是上海! 很多人说深圳才是第二,上海没有产生BAT之类的企业。 diff --git a/problems/前序/关于空间复杂度,可能有几个疑问?.md b/problems/前序/关于空间复杂度,可能有几个疑问?.md index 5b2f73e2..510979c9 100644 --- a/problems/前序/关于空间复杂度,可能有几个疑问?.md +++ b/problems/前序/关于空间复杂度,可能有几个疑问?.md @@ -10,9 +10,9 @@ # 空间复杂度分析 -* [关于时间复杂度,你不知道的都在这里!](https://mp.weixin.qq.com/s/LWBfehW1gMuEnXtQjJo-sw) -* [O(n)的算法居然超时了,此时的n究竟是多大?](https://mp.weixin.qq.com/s/73ryNsuPFvBQkt6BbhNzLA) -* [通过一道面试题目,讲一讲递归算法的时间复杂度!](https://mp.weixin.qq.com/s/I6ZXFbw09NR31F5CJR_geQ) +* [关于时间复杂度,你不知道的都在这里!](https://programmercarl.com/前序/关于时间复杂度,你不知道的都在这里!.html) +* [O(n)的算法居然超时了,此时的n究竟是多大?](https://programmercarl.com/前序/On的算法居然超时了,此时的n究竟是多大?.html) +* [通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html) 那么一直还没有讲空间复杂度,所以打算陆续来补上,内容不难,大家可以读一遍文章就有整体的了解了。 diff --git a/problems/前序/力扣上的代码想在本地编译运行?.md b/problems/前序/力扣上的代码想在本地编译运行?.md index a0fcbcd8..970af7ff 100644 --- a/problems/前序/力扣上的代码想在本地编译运行?.md +++ b/problems/前序/力扣上的代码想在本地编译运行?.md @@ -23,7 +23,7 @@ 其实挺简单的,大家看一遍就会了。 -我拿我们刚讲过的这道题[动态规划:使用最小花费爬楼梯](https://mp.weixin.qq.com/s/djZB9gkyLFAKcQcSvKDorA)来做示范。 +我拿我们刚讲过的这道题[动态规划:使用最小花费爬楼梯](https://programmercarl.com/0746.使用最小花费爬楼梯.html)来做示范。 力扣746. 使用最小花费爬楼梯,完整的可以在直接本地运行的C++代码如下: diff --git a/problems/前序/杭州互联网公司总结.md b/problems/前序/杭州互联网公司总结.md index aebf5779..326a176b 100644 --- a/problems/前序/杭州互联网公司总结.md +++ b/problems/前序/杭州互联网公司总结.md @@ -67,11 +67,10 @@ * 大搜车(总部)中国领先的汽车交易服务供应商 * 二更(总部)自媒体 * 丁香园(总部) - ## 总结 -杭州距离上海非常近,难免不了和上海做对比,上海是金融之都,如果看了[上海有这些互联网公司,你都知道么?](https://mp.weixin.qq.com/s/iW4_rXQzc0fJDuSmPTUVdQ)就会发现上海互联网也是仅次于北京的。 +杭州距离上海非常近,难免不了和上海做对比,上海是金融之都,如果看了[上海有这些互联网公司,你都知道么?](https://programmercarl.com/前序/上海互联网公司总结.html)就会发现上海互联网也是仅次于北京的。 而杭州是阿里的大本营,到处都有阿里的影子,虽然有网易在,但是也基本是盖过去了,很多中小公司也都是阿里某某高管出来创业的。 diff --git a/problems/前序/递归算法的时间与空间复杂度分析.md b/problems/前序/递归算法的时间与空间复杂度分析.md index b6c79f25..27b9d7cc 100644 --- a/problems/前序/递归算法的时间与空间复杂度分析.md +++ b/problems/前序/递归算法的时间与空间复杂度分析.md @@ -9,7 +9,7 @@ # 递归算法的时间与空间复杂度分析! -之前在[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://mp.weixin.qq.com/s/I6ZXFbw09NR31F5CJR_geQ)中详细讲解了递归算法的时间复杂度,但没有讲空间复杂度。 +之前在[通过一道面试题目,讲一讲递归算法的时间复杂度!](https://programmercarl.com/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.html)中详细讲解了递归算法的时间复杂度,但没有讲空间复杂度。 本篇讲通过求斐波那契数列和二分法再来深入分析一波递归算法的时间和空间复杂度,细心看完,会刷新对递归的认知!