From f93ad90c69237b706dc3d0c82b2ba598275e5c9e Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 26 Aug 2021 20:04:48 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index b6fa4a98..f377b99e 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,6 +133,25 @@ 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 From 1b915d57bf54c46f070b2852f12407e5ea2d6492 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 26 Aug 2021 20:14:24 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2Python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index f377b99e..1e1c4afe 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -156,6 +156,7 @@ class Solution { Python: ```python3 class Solution: + # 思路1:优先考虑胃饼干 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -165,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 //排序后,局部最优 From 31dfca184b56926c5c53805d28068efb0986e189 Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Thu, 26 Aug 2021 20:32:58 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=AD=A30027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0Rust=E8=AA=9E=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 根據題目須回傳i32而非&mut Vec 2. 改以Owner介紹的雙指針法解題 --- problems/0027.移除元素.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index fadd4d7e..dc913494 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -201,19 +201,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)); } ``` From 1ded7e92b975da71d32dd752d3b98816be3b2d30 Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Thu, 26 Aug 2021 21:15:58 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A00209.=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84Rust?= =?UTF-8?q?=E8=AA=9E=E8=A8=80=E5=AF=A6=E7=8F=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note - leetcode上提交通過 --- problems/0209.长度最小的子数组.md | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 + } +} +``` ----------------------- From d6531b0f2b9699031087ad660ea6744e5c36508a Mon Sep 17 00:00:00 2001 From: ironartisan Date: Fri, 27 Aug 2021 21:25:07 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00541=E7=BF=BB=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0541.反转字符串II.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 02713c65..914fba23 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: From 51f217b501c06a25dfaab7eb5b6bf59b8435cbb2 Mon Sep 17 00:00:00 2001 From: XuDaHaoRen <1547794387@qq.com> Date: Fri, 27 Aug 2021 23:02:34 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/前序/上海互联网公司总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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之类的企业。 From 9021e6815bc8412a231c20b94af7e30a4571324d Mon Sep 17 00:00:00 2001 From: XuDaHaoRen <1547794387@qq.com> Date: Fri, 27 Aug 2021 23:08:09 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../关于空间复杂度,可能有几个疑问?.md | 6 +++--- .../前序/力扣上的代码想在本地编译运行?.md | 2 +- problems/前序/杭州互联网公司总结.md | 3 +-- .../前序/递归算法的时间与空间复杂度分析.md | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) 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)中详细讲解了递归算法的时间复杂度,但没有讲空间复杂度。 本篇讲通过求斐波那契数列和二分法再来深入分析一波递归算法的时间和空间复杂度,细心看完,会刷新对递归的认知! From 48ab781c4e6c3d8c6ddf9610f5adda300884b291 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sat, 28 Aug 2021 09:39:16 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BC=98=E5=8C=960122.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?II=20Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0122.买卖股票的最佳时机II.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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; } } ```