From 3e506df04426ca8937f09f7a8e9ca9955d0d1c7e Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 10:56:21 +0800 Subject: [PATCH 01/14] =?UTF-8?q?update=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D=EF=BC=8Cpython?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=9B=B4pythonic?= =?UTF-8?q?=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index ba942f70..c3e73730 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -130,7 +130,27 @@ class Solution: return True ``` +Python写法二: + +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import defaultdict + + s_dict=defaultdict(int) + t_dict=defaultdict(int) + + for x in s: + s_dict[x]+=1 + + for x in t: + t_dict[x]+=1 + + return s_dict==t_dict +``` + Go: + ```go func isAnagram(s string, t string) bool { if len(s)!=len(t){ From 0d3b8de51227c0c55ace89aa249e0a25377a62be Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 10:58:05 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index c3e73730..1956253a 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -137,16 +137,16 @@ class Solution: def isAnagram(self, s: str, t: str) -> bool: from collections import defaultdict - s_dict=defaultdict(int) - t_dict=defaultdict(int) + s_dict = defaultdict(int) + t_dict = defaultdict(int) for x in s: - s_dict[x]+=1 + s_dict[x] += 1 for x in t: - t_dict[x]+=1 + t_dict[x] += 1 - return s_dict==t_dict + return s_dict == t_dict ``` Go: From 4993e9ff6bfab26473eeb9b99f07ae544f0afca3 Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 11:14:21 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E6=9C=89=E6=95=88=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 1956253a..93bba44c 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -130,7 +130,7 @@ class Solution: return True ``` -Python写法二: +Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路): ```python class Solution: From 2c03dfc3f33085a9e5440d7f26b2dc84f5514d47 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Sun, 13 Jun 2021 13:29:59 +0800 Subject: [PATCH 04/14] =?UTF-8?q?feat(0707):=20=E6=96=B0=E5=A2=9Ejava=20?= =?UTF-8?q?=E5=8F=8C=E9=93=BE=E8=A1=A8=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 2fa1af29..7d5fde1f 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -158,6 +158,7 @@ private: Java: ```Java +//单链表 class ListNode { int val; ListNode next; @@ -236,6 +237,106 @@ class MyLinkedList { pred.next = pred.next.next; } } + +//双链表 +class MyLinkedList { + class ListNode { + int val; + ListNode next,prev; + ListNode(int x) {val = x;} + } + + int size; + ListNode head,tail;//Sentinel node + + /** Initialize your data structure here. */ + public MyLinkedList() { + size = 0; + head = new ListNode(0); + tail = new ListNode(0); + head.next = tail; + tail.prev = head; + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + public int get(int index) { + if(index < 0 || index >= size){return -1;} + ListNode cur = head; + + // 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率 + if(index < (size - 1) / 2){ + for(int i = 0; i <= index; i++){ + cur = cur.next; + } + }else{ + cur = tail; + for(int i = 0; i <= size - index - 1; i++){ + cur = cur.prev; + } + } + return cur.val; + } + + /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */ + public void addAtHead(int val) { + ListNode cur = head; + ListNode newNode = new ListNode(val); + newNode.next = cur.next; + cur.next.prev = newNode; + cur.next = newNode; + newNode.prev = cur; + size++; + } + + /** Append a node of value val to the last element of the linked list. */ + public void addAtTail(int val) { + ListNode cur = tail; + ListNode newNode = new ListNode(val); + newNode.next = tail; + newNode.prev = cur.prev; + cur.prev.next = newNode; + cur.prev = newNode; + size++; + } + + /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */ + public void addAtIndex(int index, int val) { + if(index > size){return;} + if(index < 0){index = 0;} + ListNode cur = head; + for(int i = 0; i < index; i++){ + cur = cur.next; + } + ListNode newNode = new ListNode(val); + newNode.next = cur.next; + cur.next.prev = newNode; + newNode.prev = cur; + cur.next = newNode; + size++; + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + public void deleteAtIndex(int index) { + if(index >= size || index < 0){return;} + ListNode cur = head; + for(int i = 0; i < index; i++){ + cur = cur.next; + } + cur.next.next.prev = cur; + cur.next = cur.next.next; + size--; + } +} + +/** + * Your MyLinkedList object will be instantiated and called as such: + * MyLinkedList obj = new MyLinkedList(); + * int param_1 = obj.get(index); + * obj.addAtHead(val); + * obj.addAtTail(val); + * obj.addAtIndex(index,val); + * obj.deleteAtIndex(index); + */ ``` Python: From f16d590a7f2015fcee1854fb906a220091c58135 Mon Sep 17 00:00:00 2001 From: Ysc Date: Sun, 13 Jun 2021 18:40:02 +0900 Subject: [PATCH 05/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9=20Javasc?= =?UTF-8?q?ript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index d2e1f950..0f9007d7 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -198,6 +198,31 @@ impl Solution { } } ``` +Javascript: +```Javascript +/** + * @desc two pointers solution + * @link https://leetcode-cn.com/problems/squares-of-a-sorted-array/ + * @param nums Array e.g. [-4,-1,0,3,10] + * @return {array} e.g. [0,1,9,16,100] + */ +const sortedSquares = function (nums) { + let res = [] + for (let i = 0, j = nums.length - 1; i <= j;) { + const left = Math.abs(nums[i]) + const right = Math.abs(nums[j]) + if (right > left) { + // push element to the front of the array + res.unshift(right * right) + j-- + } else { + res.unshift(left * left) + i++ + } + } + return res + } +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 56d9af976a6df55808ebf8e834ed6cac7bfc38e1 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 20:30:46 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200106.=E4=BB=8E?= =?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0106.从中序与后序遍历序列构造二叉树 go版本 --- ...序与后序遍历序列构造二叉树.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index ba2d46a1..600a38e0 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -693,6 +693,70 @@ class Solution: return root ``` Go: +> 106 从中序与后序遍历序列构造二叉树 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(inorder []int, postorder []int) *TreeNode { + if len(inorder)<1||len(postorder)<1{return nil} + //先找到根节点(后续遍历的最后一个就是根节点) + nodeValue:=postorder[len(postorder)-1] + //从中序遍历中找到一分为二的点,左边为左子树,右边为右子树 + left:=findRootIndex(inorder,nodeValue) + //构造root + root:=&TreeNode{Val: nodeValue, + Left: buildTree(inorder[:left],postorder[:left]),//将后续遍历一分为二,左边为左子树,右边为右子树 + Right: buildTree(inorder[left+1:],postorder[left:len(postorder)-1])} + return root +} +func findRootIndex(inorder []int,target int) (index int){ + for i:=0;i 105 从前序与中序遍历序列构造二叉树 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func buildTree(preorder []int, inorder []int) *TreeNode { + if len(preorder)<1||len(inorder)<1{return nil} + left:=findRootIndex(preorder[0],inorder) + root:=&TreeNode{ + Val: preorder[0], + Left: buildTree(preorder[1:left+1],inorder[:left]), + Right: buildTree(preorder[left+1:],inorder[left+1:])} + return root +} +func findRootIndex(target int,inorder []int) int{ + for i:=0;i Date: Sun, 13 Jun 2021 20:48:38 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0654.最大二叉树 go版本 --- problems/0654.最大二叉树.md | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index ea8c15cd..f0d3e594 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -278,6 +278,39 @@ class Solution: Go: +> 654. 最大二叉树 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func constructMaximumBinaryTree(nums []int) *TreeNode { + if len(nums)<1{return nil} + //首选找到最大值 + index:=findMax(nums) + //其次构造二叉树 + root:=&TreeNode{ + Val: nums[index], + Left:constructMaximumBinaryTree(nums[:index]),//左半边 + Right:constructMaximumBinaryTree(nums[index+1:]),//右半边 + } + return root +} +func findMax(nums []int) (index int){ + for i:=0;inums[index]{ + index=i + } + } + return +} +``` + From 0e267fda96857c884f8cf920c20f00f4f5d1e6a3 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 20:56:40 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200739.=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E6=B8=A9=E5=BA=A6=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0739.每日温度 go版本 --- problems/0739.每日温度.md | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index 1f91e42a..eeea6ead 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -214,6 +214,52 @@ Python: Go: +> 暴力法 + +```go +func dailyTemperatures(temperatures []int) []int { + length:=len(temperatures) + res:=make([]int,length) + for i:=0;i=temperatures[j]{//大于等于 + j++ + } + if j 单调栈法 + +```go +func dailyTemperatures(temperatures []int) []int { + length:=len(temperatures) + res:=make([]int,length) + stack:=[]int{} + for i:=0;i0&&temperatures[i]>temperatures[stack[len(stack)-1]]{ + res[stack[len(stack)-1]]=i-stack[len(stack)-1]//存放结果集 + stack=stack[:len(stack)-1]//删除stack[len(stack)-1]的元素 + } + //如果栈顶元素大于等于新来的元素,则加入到栈中。当栈内元素个数为0时,直接入栈 + if len(stack)==0||temperatures[i]<=temperatures[stack[len(stack)-1]]{ + stack = append(stack, i) + } + } + return res +} +``` From 01f87cc50566c9a171f6c828d1eb2b9f5828f646 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 21:48:05 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200617.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0617.合并二叉树 go版本 --- problems/0617.合并二叉树.md | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index d65275f0..be6bb425 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -332,6 +332,43 @@ class Solution: Go: +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //前序遍历(递归遍历,跟105 106差不多的思路) +func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode { + var value int + var nullNode *TreeNode//空node,便于遍历 + nullNode=&TreeNode{ + Val:0, + Left:nil, + Right:nil} + switch { + case t1==nil&&t2==nil: return nil//终止条件 + default : //如果其中一个节点为空,则将该节点置为nullNode,方便下次遍历 + if t1==nil{ + value=t2.Val + t1=nullNode + }else if t2==nil{ + value=t1.Val + t2=nullNode + }else { + value=t1.Val+t2.Val + } + } + root:=&TreeNode{//构造新的二叉树 + Val: value, + Left: mergeTrees(t1.Left,t2.Left), + Right: mergeTrees(t1.Right,t2.Right)} + return root +} +``` From ac1e326cbf15017d2a98d53620016f3bb6069421 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 22:17:57 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200700.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0700.二叉搜索树中的搜索 go版本 --- problems/0700.二叉搜索树中的搜索.md | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 25a06617..16b21f26 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -241,6 +241,56 @@ class Solution: Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //递归法 +func searchBST(root *TreeNode, val int) *TreeNode { + if root==nil||root.Val==val{ + return root + } + if root.Val>val{ + return searchBST(root.Left,val) + } + return searchBST(root.Right,val) +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //迭代法 +func searchBST(root *TreeNode, val int) *TreeNode { + for root!=nil{ + if root.Val>val{ + root=root.Left + }else if root.Val Date: Mon, 14 Jun 2021 12:58:03 +0800 Subject: [PATCH 11/14] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A=E9=87=8D?= =?UTF-8?q?=E8=83=8C=E5=8C=85.md=20=E6=B7=BB=E5=8A=A0=20python3=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础多重背包.md | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index 26890a2b..e14575d4 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -147,9 +147,56 @@ int main() { Java: - Python: +```python +def test_multi_pack1(): + '''版本一:改变物品数量为01背包格式''' + weight = [1, 3, 4] + value = [15, 20, 30] + nums = [2, 3, 2] + bag_weight = 10 + for i in range(len(nums)): + # 将物品展开数量为1 + while nums[i] > 1: + weight.append(weight[i]) + value.append(value[i]) + nums[i] -= 1 + + dp = [0]*(bag_weight + 1) + # 遍历物品 + for i in range(len(weight)): + # 遍历背包 + for j in range(bag_weight, weight[i] - 1, -1): + dp[j] = max(dp[j], dp[j - weight[i]] + value[i]) + + print(" ".join(map(str, dp))) + +def test_multi_pack2(): + '''版本:改变遍历个数''' + weight = [1, 3, 4] + value = [15, 20, 30] + nums = [2, 3, 2] + bag_weight = 10 + + dp = [0]*(bag_weight + 1) + for i in range(len(weight)): + for j in range(bag_weight, weight[i] - 1, -1): + # 以上是01背包,加上遍历个数 + for k in range(1, nums[i] + 1): + if j - k*weight[i] >= 0: + dp[j] = max(dp[j], dp[j - k*weight[i]] + k*value[i]) + + print(" ".join(map(str, dp))) + + +if __name__ == '__main__': + test_multi_pack1() + test_multi_pack2() +``` + + + Go: From a47eddd56feb76cd95eae8d02fee30b5740c9ade Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Mon, 14 Jun 2021 21:56:40 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0530.二叉搜索树的最小绝对差 go版本 --- .../0530.二叉搜索树的最小绝对差.md | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 03c48d9c..0bbc4908 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -224,8 +224,37 @@ class Solution: return r ``` Go: +> 中序遍历,然后计算最小差值 - +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func getMinimumDifference(root *TreeNode) int { + var res []int + findMIn(root,&res) + min:=1000000//一个比较大的值 + for i:=1;i Date: Mon, 14 Jun 2021 21:00:34 -0400 Subject: [PATCH 13/14] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java solution --- problems/0115.不同的子序列.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index e54389aa..d3bc6d97 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -145,7 +145,28 @@ public: Java: - +```java +class Solution { + public int numDistinct(String s, String t) { + int[][] dp = new int[s.length() + 1][t.length() + 1]; + for (int i = 0; i < s.length() + 1; i++) { + dp[i][0] = 1; + } + + for (int i = 1; i < s.length() + 1; i++) { + for (int j = 1; j < t.length() + 1; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + }else{ + dp[i][j] = dp[i - 1][j]; + } + } + } + + return dp[s.length()][t.length()]; + } +} +``` Python: ```python From b1407c02a48f53dde729dea361c41ab283e72fb1 Mon Sep 17 00:00:00 2001 From: Yang Date: Mon, 14 Jun 2021 22:09:33 -0400 Subject: [PATCH 14/14] =?UTF-8?q?Update=200583.=E4=B8=A4=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java solution --- .../0583.两个字符串的删除操作.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 0b32d129..ee1fbc5f 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -104,6 +104,28 @@ public: Java: +```java +class Solution { + public int minDistance(String word1, String word2) { + int[][] dp = new int[word1.length() + 1][word2.length() + 1]; + for (int i = 0; i < word1.length() + 1; i++) dp[i][0] = i; + for (int j = 0; j < word2.length() + 1; j++) dp[0][j] = j; + + for (int i = 1; i < word1.length() + 1; i++) { + for (int j = 1; j < word2.length() + 1; j++) { + if (word1.charAt(i - 1) == word2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + }else{ + dp[i][j] = Math.min(dp[i - 1][j - 1] + 2, + Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1)); + } + } + } + + return dp[word1.length()][word2.length()]; + } +} +``` Python: