diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 8ee1d07b..a1e4f020 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -218,8 +218,33 @@ class Solution { ``` Python: - - +```Python +class Solution: + def threeSum(self, nums): + ans = [] + n = len(nums) + nums.sort() + for i in range(n): + left = i + 1 + right = n - 1 + if nums[i] > 0: + break + if i >= 1 and nums[i] == nums[i - 1]: + continue + while left < right: + total = nums[i] + nums[left] + nums[right] + if total > 0: + right -= 1 + elif total < 0: + left += 1 + else: + ans.append([nums[i], nums[left], nums[right]]) + while left != right and nums[left] == nums[left + 1]: left += 1 + while left != right and nums[right] == nums[right - 1]: right -= 1 + left += 1 + right -= 1 + return ans +``` Go: ```Go func threeSum(nums []int)[][]int{ diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index e84a1634..69bccec5 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -168,7 +168,21 @@ class Solution { ``` Python: - +```python +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + if len(intervals) == 0: return intervals + intervals.sort(key=lambda x: x[0]) + result = [] + result.append(intervals[0]) + for i in range(1, len(intervals)): + last = result[-1] + if last[1] >= intervals[i][0]: + result[-1] = [last[0], max(last[1], intervals[i][1])] + else: + result.append(intervals[i]) + return result +``` Go: @@ -179,4 +193,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index e3a6da8c..680bba83 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -249,7 +249,24 @@ Python: Go: - +```Go +func uniquePaths(m int, n int) int { + dp := make([][]int, m) + for i := range dp { + dp[i] = make([]int, n) + dp[i][0] = 1 + } + for j := 0; j < n; j++ { + dp[0][j] = 1 + } + for i := 1; i < m; i++ { + for j := 1; j < n; j++ { + dp[i][j] = dp[i-1][j] + dp[i][j-1] + } + } + return dp[m-1][n-1] +} +``` diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 1cb4164f..c40f4e2a 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -79,6 +79,35 @@ public: return result; } }; +``` +javascript代码: + +```javascript +var levelOrder = function(root) { + //二叉树的层序遍历 + let res=[],queue=[]; + queue.push(root); + if(root===null){ + return res; + } + while(queue.length!==0){ + // 记录当前层级节点数 + let length=queue.length; + //存放每一层的节点 + let curLevel=[]; + for(let i=0;inode.val?max:node.val; + node.left&&queue.push(node.left); + node.right&&queue.push(node.right); + } + //把每一层的最大值放到res数组 + res.push(max); + } + return res; +}; +``` ## 116.填充每个节点的下一个右侧节点指针 diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 93dc5fd6..139c3dae 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -233,7 +233,27 @@ class Solution { ``` Python: - +```python3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +#递归法 +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> TreeNode: + def buildaTree(left,right): + if left > right: return None #左闭右闭的区间,当区间 left > right的时候,就是空节点,当left = right的时候,不为空 + mid = left + (right - left) // 2 #保证数据不会越界 + val = nums[mid] + root = TreeNode(val) + root.left = buildaTree(left,mid - 1) + root.right = buildaTree(mid + 1,right) + return root + root = buildaTree(0,len(nums) - 1) #左闭右闭区间 + return root +``` Go: @@ -244,4 +264,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index d5ad368d..c52c5f9e 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -172,6 +172,30 @@ Python: Go: +```go +func minSubArrayLen(target int, nums []int) int { + i := 0 + l := len(nums) // 数组长度 + sum := 0 // 子数组之和 + result := l + 1 // 初始化返回长度为l+1,目的是为了判断“不存在符合条件的子数组,返回0”的情况 + for j := 0; j < l; j++ { + sum += nums[j] + for sum >= target { + subLength := j - i + 1 + if subLength < result { + result = subLength + } + sum -= nums[i] + i++ + } + } + if result == l+1 { + return 0 + } else { + return result + } +} +``` JavaScript: @@ -200,4 +224,4 @@ var minSubArrayLen = function(target, nums) { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 209c989b..cc19bb30 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -196,8 +196,26 @@ class Solution { ``` Python: - - +```python3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +#递归法 +class Solution: + def convertBST(self, root: TreeNode) -> TreeNode: + def buildalist(root): + if not root: return None + buildalist(root.right) #右中左遍历 + root.val += self.pre + self.pre = root.val + buildalist(root.left) + self.pre = 0 #记录前一个节点的数值 + buildalist(root) + return root +``` Go: @@ -207,4 +225,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 8b9e1f6d..5c839b6c 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -265,8 +265,24 @@ class Solution { ``` Python: - - +```python3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode: + if not root: return root + if root.val < low: + return self.trimBST(root.right,low,high) // 寻找符合区间[low, high]的节点 + if root.val > high: + return self.trimBST(root.left,low,high) // 寻找符合区间[low, high]的节点 + root.left = self.trimBST(root.left,low,high) // root->left接入符合条件的左孩子 + root.right = self.trimBST(root.right,low,high) // root->right接入符合条件的右孩子 + return root +``` Go: @@ -276,4 +292,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index dc136028..4324a0bc 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -8,6 +8,7 @@ ## 738.单调递增的数字 +题目链接: https://leetcode-cn.com/problems/monotone-increasing-digits/ 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增。 @@ -30,7 +31,7 @@ ## 暴力解法 -题意很简单,那么首先想的就是暴力解法了,来我提大家暴力一波,结果自然是超时! +题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时! 代码如下: ```C++ @@ -146,7 +147,19 @@ class Solution { Python: - +```python +class Solution: + def monotoneIncreasingDigits(self, n: int) -> int: + strNum = list(str(n)) + flag = len(strNum) + for i in range(len(strNum) - 1, 0, -1): + if int(strNum[i]) < int(strNum[i - 1]): + strNum[i - 1] = str(int(strNum[i - 1]) - 1) + flag = i + for i in range(flag, len(strNum)): + strNum[i] = '9' + return int("".join(strNum)) +``` Go: @@ -157,4 +170,4 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index c1474280..a6ca0ea0 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -108,7 +108,23 @@ class Solution { ``` Python: +```python +class Solution: + def partitionLabels(self, s: str) -> List[int]: + hash = [0] * 26 + for i in range(len(s)): + hash[ord(s[i]) - ord('a')] = i + result = [] + left = 0 + right = 0 + for i in range(len(s)): + right = max(right, hash[ord(s[i]) - ord('a')]) + if i == right: + result.append(right - left + 1) + left = i + 1 + return result +``` Go: diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index bc91eca0..bf3e83f4 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -242,7 +242,137 @@ Python: Go: +> 前序遍历统一迭代法 +```GO + /** + type Element struct { + // 元素保管的值 + Value interface{} + // 内含隐藏或非导出字段 +} + +func (l *List) Back() *Element +前序遍历:中左右 +压栈顺序:右左中 + **/ +func preorderTraversal(root *TreeNode) []int { + if root == nil { + return nil + } + var stack = list.New()//栈 + res:=[]int{}//结果集 + stack.PushBack(root) + var node *TreeNode + for stack.Len()>0{ + e := stack.Back() + stack.Remove(e)//弹出元素 + if e.Value==nil{// 如果为空,则表明是需要处理中间节点 + e=stack.Back()//弹出元素(即中间节点) + stack.Remove(e)//删除中间节点 + node=e.Value.(*TreeNode) + res=append(res,node.Val)//将中间节点加入到结果集中 + continue//继续弹出栈中下一个节点 + } + node = e.Value.(*TreeNode) + //压栈顺序:右左中 + if node.Right!=nil{ + stack.PushBack(node.Right) + } + if node.Left!=nil{ + stack.PushBack(node.Left) + } + stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符 + stack.PushBack(nil) + } + return res + +} +``` + +> 中序遍历统一迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //中序遍历:左中右 + //压栈顺序:右中左 +func inorderTraversal(root *TreeNode) []int { + if root==nil{ + return nil + } + stack:=list.New()//栈 + res:=[]int{}//结果集 + stack.PushBack(root) + var node *TreeNode + for stack.Len()>0{ + e := stack.Back() + stack.Remove(e) + if e.Value==nil{// 如果为空,则表明是需要处理中间节点 + e=stack.Back()//弹出元素(即中间节点) + stack.Remove(e)//删除中间节点 + node=e.Value.(*TreeNode) + res=append(res,node.Val)//将中间节点加入到结果集中 + continue//继续弹出栈中下一个节点 + } + node = e.Value.(*TreeNode) + //压栈顺序:右中左 + if node.Right!=nil{ + stack.PushBack(node.Right) + } + stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符 + stack.PushBack(nil) + if node.Left!=nil{ + stack.PushBack(node.Left) + } + } + return res +} +``` + +> 后序遍历统一迭代法 + +```go +//后续遍历:左右中 +//压栈顺序:中右左 +func postorderTraversal(root *TreeNode) []int { + if root == nil { + return nil + } + var stack = list.New()//栈 + res:=[]int{}//结果集 + stack.PushBack(root) + var node *TreeNode + for stack.Len()>0{ + e := stack.Back() + stack.Remove(e) + if e.Value==nil{// 如果为空,则表明是需要处理中间节点 + e=stack.Back()//弹出元素(即中间节点) + stack.Remove(e)//删除中间节点 + node=e.Value.(*TreeNode) + res=append(res,node.Val)//将中间节点加入到结果集中 + continue//继续弹出栈中下一个节点 + } + node = e.Value.(*TreeNode) + //压栈顺序:中右左 + stack.PushBack(node)//中间节点压栈后再压入nil作为中间节点的标志符 + stack.PushBack(nil) + if node.Right!=nil{ + stack.PushBack(node.Right) + } + if node.Left!=nil{ + stack.PushBack(node.Left) + } + } + return res +} +``` diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 3e9ab11f..9bd639aa 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -96,10 +96,27 @@ public: ## 其他语言版本 - Java: - - +```java +class Solution { + public String reverseLeftWords(String s, int n) { + int len=s.length(); + StringBuilder sb=new StringBuilder(s); + reverseString(sb,0,n-1); + reverseString(sb,n,len-1); + return sb.reverse().toString(); + } + public void reverseString(StringBuilder sb, int start, int end) { + while (start < end) { + char temp = sb.charAt(start); + sb.setCharAt(start, sb.charAt(end)); + sb.setCharAt(end, temp); + start++; + end--; + } + } +} +``` Python: