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: 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/ 收藏一下,平时用到哪里了忘了就看一看。 就酱,后面我还会陆续给大家安利一些编程利器。 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)