From 80dda7ac0918d857d969a27f278e882886ec98f8 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Sat, 17 Jul 2021 14:37:00 -0700 Subject: [PATCH 1/6] add js solution for longestPalindromeSubseq --- problems/0516.最长回文子序列.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index a4f0522f..388d8d6a 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -214,7 +214,29 @@ func longestPalindromeSubseq(s string) int { } ``` +Javascript: +```javascript +const longestPalindromeSubseq = (s) => { + const strLen = s.length; + let dp = Array.from(Array(strLen), () => Array(strLen).fill(0)); + + for(let i = 0; i < strLen; i++) { + dp[i][i] = 1; + } + for(let i = strLen - 1; i >= 0; i--) { + for(let j = i + 1; j < strLen; j++) { + if(s[i] === s[j]) { + dp[i][j] = dp[i+1][j-1] + 2; + } else { + dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]); + } + } + } + + return dp[0][strLen - 1]; +}; +``` ----------------------- From 01f14ae547f6af6a68babeaa4bb562ee14253309 Mon Sep 17 00:00:00 2001 From: daniel1n <54945782+daniel1n@users.noreply.github.com> Date: Sun, 18 Jul 2021 13:35:20 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Update=200406.=E6=A0=B9=E6=8D=AE=E8=BA=AB?= =?UTF-8?q?=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java:使用lambda --- problems/0406.根据身高重建队列.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md index 2b447da4..a5f66a5d 100644 --- a/problems/0406.根据身高重建队列.md +++ b/problems/0406.根据身高重建队列.md @@ -188,15 +188,10 @@ Java: ```java class Solution { public int[][] reconstructQueue(int[][] people) { - Arrays.sort(people, new Comparator() { - @Override - public int compare(int[] o1, int[] o2) { - if (o1[0] != o2[0]) { - return Integer.compare(o2[0],o1[0]); - } else { - return Integer.compare(o1[1],o2[1]); - } - } + // 身高从大到小排(身高相同k小的站前面) + Arrays.sort(people, (a, b) -> { + if (a[0] == b[0]) return a[1] - b[1]; + return b[0] - a[0]; }); LinkedList que = new LinkedList<>(); From e7941a7ad75353fac772c2f1b3ae84af20d6ed75 Mon Sep 17 00:00:00 2001 From: Gasoonjia Date: Sun, 18 Jul 2021 22:00:36 -0400 Subject: [PATCH 3/6] =?UTF-8?q?Update=200024.=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0024.两两交换链表中的节点.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 59ded523..66e149e6 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -9,6 +9,8 @@ ## 24. 两两交换链表中的节点 +https://leetcode-cn.com/problems/swap-nodes-in-pairs/ + 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 From 08cad024bac1d45b6b877b7f0ca9d2fb57537e95 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Tue, 20 Jul 2021 09:56:43 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200503.=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II=20pyth?= =?UTF-8?q?on3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0503.下一个更大元素II python3版本 --- problems/0503.下一个更大元素II.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md index 3980cb0e..34ade48e 100644 --- a/problems/0503.下一个更大元素II.md +++ b/problems/0503.下一个更大元素II.md @@ -97,7 +97,18 @@ public: Java: Python: - +```python3 +class Solution: + def nextGreaterElements(self, nums: List[int]) -> List[int]: + dp = [-1] * len(nums) + stack = [] + for i in range(len(nums)*2): + while(len(stack) != 0 and nums[i%len(nums)] > nums[stack[-1]]): + dp[stack[-1]] = nums[i%len(nums)] + stack.pop() + stack.append(i%len(nums)) + return dp +``` Go: JavaScript: From 1b3d8ca75ce58e7e392a5042d8cf14af9719e1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=82=E8=80=83=E7=AD=94=E6=A1=88=E5=BC=80=E5=BF=83?= =?UTF-8?q?=E5=90=A6?= <46703189+wp19991@users.noreply.github.com> Date: Tue, 20 Jul 2021 21:55:40 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Update=20=E7=A8=8B=E5=BA=8F=E5=91=98?= =?UTF-8?q?=E5=86=99=E6=96=87=E6=A1=A3=E5=B7=A5=E5=85=B7.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 直接点击链接会报错,增加了空格之后iu就可以了 --- problems/前序/程序员写文档工具.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/前序/程序员写文档工具.md b/problems/前序/程序员写文档工具.md index 5530e30f..b06ce0ad 100644 --- a/problems/前序/程序员写文档工具.md +++ b/problems/前序/程序员写文档工具.md @@ -124,7 +124,7 @@ Markdown支持部分html,例如这样 我这里仅仅是介绍了几个常用的语法,刚开始学习Markdown的时候语法难免会忘。 -所以建议把这个markdown demo:https://markdown-it.github.io/收藏一下,平时用到哪里了忘了就看一看。 +所以建议把这个markdown demo:https://markdown-it.github.io/ 收藏一下,平时用到哪里了忘了就看一看。 就酱,后面我还会陆续给大家安利一些编程利器。 From 183b1f8316b4aec2ed4aeb4494955036ab416513 Mon Sep 17 00:00:00 2001 From: Gasoonjia Date: Tue, 20 Jul 2021 22:31:07 -0400 Subject: [PATCH 6/6] =?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)