From 183b1f8316b4aec2ed4aeb4494955036ab416513 Mon Sep 17 00:00:00 2001 From: Gasoonjia Date: Tue, 20 Jul 2021 22:31:07 -0400 Subject: [PATCH 01/10] =?UTF-8?q?Update=20=E5=89=91=E6=8C=87Offer05.?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer05.替换空格.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index f68d8e22..49c2c223 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -199,6 +199,52 @@ func replaceSpace(s string) string { + +python: +```python +class Solution(object): + def replaceSpace(self, s): + """ + :type s: str + :rtype: str + """ + list_s = list(s) + + # 记录原本字符串的长度 + original_end = len(s) + + # 将空格改成%20 使得字符串总长增长 2n,n为原本空格数量。 + # 所以记录空格数量就可以得到目标字符串的长度 + n_space = 0 + for ss in s: + if ss == ' ': + n_space += 1 + + list_s += ['0'] * 2 * n_space + + # 设置左右指针位置 + left, right = original_end - 1, len(list_s) - 1 + + # 循环直至左指针越界 + while left >= 0: + if list_s[left] == ' ': + list_s[right] = '0' + list_s[right - 1] = '2' + list_s[right - 2] = '%' + right -= 3 + else: + list_s[right] = list_s[left] + right -= 1 + + left -= 1 + + # 将list变回str,输出 + s = ''.join(list_s) + return s + +``` + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 9bd3c4d3549516cd469e9b4ab8580c326741479a Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Wed, 21 Jul 2021 22:32:35 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A00455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加0455.分发饼干 go版本 --- problems/0455.分发饼干.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 4814d414..6b121e36 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -146,6 +146,23 @@ class Solution: return res ``` Go: +```golang +//排序后,局部最优 +func findContentChildren(g []int, s []int) int { + sort.Ints(g) + sort.Ints(s) + + // 从小到大 + child := 0 + for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ { + if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合 + child++ + } + } + + return child +} + Javascript: ```Javascript From 59755d57f28cedc466110fd2c6075f30bcfe0cd0 Mon Sep 17 00:00:00 2001 From: SkyLazy <627770537@qq.com> Date: Thu, 22 Jul 2021 17:19:15 +0800 Subject: [PATCH 03/10] =?UTF-8?q?Update=200239.=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index bb89a1ac..dedc3247 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -273,8 +273,8 @@ class Solution { int[] res = new int[n - k + 1]; int idx = 0; for(int i = 0; i < n; i++) { - // 根据题意,i为nums下标,是要在[i - k + 1, k] 中选到最大值,只需要保证两点 - // 1.队列头结点需要在[i - k + 1, k]范围内,不符合则要弹出 + // 根据题意,i为nums下标,是要在[i - k + 1, i] 中选到最大值,只需要保证两点 + // 1.队列头结点需要在[i - k + 1, i]范围内,不符合则要弹出 while(!deque.isEmpty() && deque.peek() < i - k + 1){ deque.poll(); } From 1176b756a9b82c282c5aa05c539402f6805d9f85 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Fri, 23 Jul 2021 10:55:16 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0376.摆动序列 go版本 --- problems/0376.摆动序列.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 4d283eb0..f64e0043 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -151,7 +151,24 @@ class Solution: ``` Go: - +```golang +func wiggleMaxLength(nums []int) int { + var count,preDiff,curDiff int + count=1 + if len(nums)<2{ + return count + } + for i:=0;i 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){ + preDiff=curDiff + count++ + } + } + return count +} +``` Javascript: ```Javascript From 65363329f02cefb67909e781c326bffc7fe5b1eb Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Fri, 23 Jul 2021 14:00:03 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 0122.买卖股票的最佳时机II go版本 --- .../0122.买卖股票的最佳时机II.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md index 4b878aa0..60d4591f 100644 --- a/problems/0122.买卖股票的最佳时机II.md +++ b/problems/0122.买卖股票的最佳时机II.md @@ -188,7 +188,40 @@ class Solution: ``` Go: +```golang +//贪心算法 +func maxProfit(prices []int) int { + var sum int + for i := 1; i < len(prices); i++ { + // 累加每次大于0的交易 + if prices[i]-prices[i-1] > 0 { + sum += prices[i]-prices[i-1] + } + } + return sum +} +``` +```golang +//确定售卖点 +func maxProfit(prices []int) int { + var result,buy int + prices=append(prices,0)//在price末尾加个0,防止price一直递增 + /** + 思路:检查后一个元素是否大于当前元素,如果小于,则表明这是一个售卖点,当前元素的值减去购买时候的值 + 如果不小于,说明后面有更好的售卖点, + **/ + for i:=0;iprices[i+1]{ + result+=prices[i]-prices[buy] + buy=i+1 + }else if prices[buy]>prices[i]{//更改最低购买点 + buy=i + } + } + return result +} +``` Javascript: ```Javascript From 7ae65550db141a1bb6f3e537d027d25b5b24b1d6 Mon Sep 17 00:00:00 2001 From: SwordsmanYao Date: Fri, 23 Jul 2021 15:26:04 +0800 Subject: [PATCH 06/10] =?UTF-8?q?[=E5=89=91=E6=8C=87Offer05.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E7=A9=BA=E6=A0=BC]=20=E6=B7=BB=E5=8A=A0=20javaScript?= =?UTF-8?q?=20=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer05.替换空格.md | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 49c2c223..3e4e3631 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -245,6 +245,45 @@ class Solution(object): ``` +javaScript: +```js +/** + * @param {string} s + * @return {string} + */ + var replaceSpace = function(s) { + // 字符串转为数组 + const strArr = Array.from(s); + let count = 0; + + // 计算空格数量 + for(let i = 0; i < strArr.length; i++) { + if (strArr[i] === ' ') { + count++; + } + } + + let left = strArr.length - 1; + let right = strArr.length + count * 2 - 1; + + while(left >= 0) { + if (strArr[left] === ' ') { + strArr[right--] = '0'; + strArr[right--] = '2'; + strArr[right--] = '%'; + left--; + } else { + strArr[right--] = strArr[left--]; + } + } + + // 数组转字符串 + return strArr.join(''); +}; +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From cd951923386f70662dd27f793008658433a0595e Mon Sep 17 00:00:00 2001 From: SwordsmanYao Date: Fri, 23 Jul 2021 15:36:03 +0800 Subject: [PATCH 07/10] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index ffa3446a..4b1778c8 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -301,6 +301,7 @@ class Solution { ``` +Python ```Python3 class Solution: #1.去除多余的空格 @@ -349,7 +350,7 @@ class Solution: return ''.join(l) #输出:blue is sky the -''' +``` Go: From dc8f9fbde59e6e891e047d1721ba8ecf1c3a6ff7 Mon Sep 17 00:00:00 2001 From: SwordsmanYao Date: Fri, 23 Jul 2021 17:09:43 +0800 Subject: [PATCH 08/10] =?UTF-8?q?[0151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D]=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20javaScript=20=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index ffa3446a..95222d2e 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -407,6 +407,65 @@ func reverse(b *[]byte, left, right int) { +javaScript: +```js +/** + * @param {string} s + * @return {string} + */ + var reverseWords = function(s) { + // 字符串转数组 + const strArr = Array.from(s); + // 移除多余空格 + removeExtraSpaces(strArr); + // 翻转 + reverse(strArr, 0, strArr.length - 1); + + let start = 0; + + for(let i = 0; i <= strArr.length; i++) { + if (strArr[i] === ' ' || i === strArr.length) { + // 翻转单词 + reverse(strArr, start, i - 1); + start = i + 1; + } + } + + return strArr.join(''); +}; + +// 删除多余空格 +function removeExtraSpaces(strArr) { + let slowIndex = 0; + let fastIndex = 0; + + while(fastIndex < strArr.length) { + // 移除开始位置和重复的空格 + if (strArr[fastIndex] === ' ' && (fastIndex === 0 || strArr[fastIndex - 1] === ' ')) { + fastIndex++; + } else { + strArr[slowIndex++] = strArr[fastIndex++]; + } + } + + // 移除末尾空格 + strArr.length = strArr[slowIndex - 1] === ' ' ? slowIndex - 1 : slowIndex; +} + +// 翻转从 start 到 end 的字符 +function reverse(strArr, start, end) { + let left = start; + let right = end; + + while(left < right) { + // 交换 + [strArr[left], strArr[right]] = [strArr[right], strArr[left]]; + left++; + right--; + } +} +``` + From 7fc26b502a1f1fe51a449db4a5a0dd5b38edd3ec Mon Sep 17 00:00:00 2001 From: Kelvin Date: Fri, 23 Jul 2021 16:09:38 -0400 Subject: [PATCH 09/10] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Python 版本 TreeNode定义. --- problems/二叉树理论基础.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index f4bb8fa9..a720672e 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -206,7 +206,13 @@ public class TreeNode { Python: - +```python +class TreeNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None +``` Go: ``` From 7b263b445d4ba5f7bc98f8b574cea17ebc1209b3 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Fri, 23 Jul 2021 23:13:36 +0200 Subject: [PATCH 10/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200042=E6=8E=A5?= =?UTF-8?q?=E9=9B=A8=E6=B0=B4=20python3=E7=89=88=E6=9C=AC=20=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0042接雨水 python3版本 双指针法 --- problems/0042.接雨水.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 55a5c522..33a90e4e 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -354,4 +354,24 @@ public: ## 其他语言版本 - +python 版本 +双指针法 +```python3 +class Solution: + def trap(self, height: List[int]) -> int: + res = 0 + for i in range(len(height)): + if i == 0 or i == len(height)-1: continue + lHight = height[i-1] + rHight = height[i+1] + for j in range(i-1): + if height[j] > lHight: + lHight = height[j] + for k in range(i+2,len(height)): + if height[k] > rHight: + rHight = height[k] + res1 = min(lHight,rHight) - height[i] + if res1 > 0: + res += res1 + return res +```