From 31e340bf6c953850705bf80701f7f9df5c6622d6 Mon Sep 17 00:00:00 2001 From: Jack_ZhijieFang <56966563+laerpeeK@users.noreply.github.com> Date: Fri, 5 Aug 2022 02:45:36 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fix:=E9=93=BE=E8=A1=A8=E7=90=86=E8=AE=BA?= =?UTF-8?q?=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typescript中,null只能赋值给any和null类型变量。这里需要采用联合类型声明。 --- problems/链表理论基础.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 1a29c32a..8378f7f2 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -186,7 +186,7 @@ TypeScript: ```typescript class ListNode { public val: number; - public next: ListNode = null; + public next: ListNode|null = null; constructor(value: number) { this.val = value; this.next = null; From 0f96289ed1485f64b010864c1ee26bd94051c132 Mon Sep 17 00:00:00 2001 From: Jack_ZhijieFang <56966563+laerpeeK@users.noreply.github.com> Date: Fri, 5 Aug 2022 03:15:03 +0800 Subject: [PATCH 2/9] =?UTF-8?q?fix=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复某个变量编译时的类型问题。 --- problems/0203.移除链表元素.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 6fd9b66f..5622fd1c 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -385,7 +385,8 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { if (cur.val === val) { pre.next = cur.next; } else { - pre = pre.next; + //此处不加类型断言时:编译器会认为pre类型为ListNode, pre.next类型为ListNode | null + pre = pre.next as ListNode; } cur = cur.next; } From ec52d094831b9b9e936cf4c17b83abc3476799dc Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Fri, 5 Aug 2022 17:11:03 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200738.=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=20Rust?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0738.单调递增的数字 Rust版本 --- problems/0738.单调递增的数字.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 6b7381f3..b452d02d 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -278,5 +278,26 @@ object Solution { } ``` +### Rust + +```Rust +impl Solution { + pub fn monotone_increasing_digits(n: i32) -> i32 { + let mut str_num = n.to_string().chars().map(|x| x.to_digit(10).unwrap() as i32).collect::>(); + let mut flag = str_num.len(); + for i in (1..str_num.len()).rev() { + if str_num[i - 1] > str_num[i] { + flag = i; + str_num[i - 1] -= 1; + } + } + for i in flag..str_num.len() { + str_num[i] = 9; + } + str_num.iter().fold(0, |acc, x| acc * 10 + x) + } +} +``` + -----------------------
From b802e83e435cb4cf23f13c699816dc2f77bf500c Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Sat, 6 Aug 2022 18:27:34 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200714.=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=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=EF=BC=89=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0714.买卖股票的最佳时机含手续费(动态规划) Rust版本 --- ...佳时机含手续费(动态规划).md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 7734450e..5ae8c33b 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -221,8 +221,46 @@ function maxProfit(prices: number[], fee: number): number { }; ``` +Rust: +**贪心** +```Rust +impl Solution { + pub fn max_profit(prices: Vec, fee: i32) -> i32 { + let mut result = 0; + let mut min_price = prices[0]; + for i in 1..prices.len() { + if prices[i] < min_price { min_price = prices[i]; } + // if prices[i] >= min_price && prices[i] <= min_price + fee { continue; } + if prices[i] > min_price + fee { + result += prices[i] - min_price - fee; + min_price = prices[i] - fee; + } + } + result + } +} +``` + +**动态规划** +```Rust +impl Solution { + fn max(a: i32, b: i32) -> i32 { + if a > b { a } else { b } + } + pub fn max_profit(prices: Vec, fee: i32) -> i32 { + let n = prices.len(); + let mut dp = vec![vec![0; 2]; n]; + dp[0][0] -= prices[0]; + for i in 1..n { + dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + } + Self::max(dp[n - 1][0], dp[n - 1][1]) + } +} +``` -----------------------
From 5868d5ec6dfd786c32cb6485e96fd861b28fccf2 Mon Sep 17 00:00:00 2001 From: Jack_ZhijieFang <56966563+laerpeeK@users.noreply.github.com> Date: Sun, 7 Aug 2022 02:11:01 +0800 Subject: [PATCH 5/9] =?UTF-8?q?Update=200019.=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原方法编译时发现对节点类型缺少断言。 --- ...0019.删除链表的倒数第N个节点.md | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 3499ab9d..62fe4f8b 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -188,18 +188,20 @@ TypeScript: ```typescript function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let newHead: ListNode | null = new ListNode(0, head); - let slowNode: ListNode | null = newHead, - fastNode: ListNode | null = newHead; - for (let i = 0; i < n; i++) { - fastNode = fastNode.next; + //根据leetcode题目的定义可推断这里快慢指针均不需要定义为ListNode | null。 + let slowNode: ListNode = newHead; + let fastNode: ListNode = newHead; + + while(n--) { + fastNode = fastNode.next!; //由虚拟头节点前进n个节点时,fastNode.next可推断不为null。 } - while (fastNode.next) { + while(fastNode.next) { //遍历直至fastNode.next = null, 即尾部节点。 此时slowNode指向倒数第n个节点。 fastNode = fastNode.next; - slowNode = slowNode.next; + slowNode = slowNode.next!; } - slowNode.next = slowNode.next.next; - return newHead.next; -}; + slowNode.next = slowNode.next!.next; //倒数第n个节点可推断其next节点不为空。 + return newHead.next; +} ``` 版本二(计算节点总数法): From 60f3ad665b005e518a6b413304bfbf26058feeb5 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Sun, 7 Aug 2022 19:34:04 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200343.=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E6=8B=86=E5=88=86=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0343.整数拆分 Rust版本 --- problems/0343.整数拆分.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index a4d532fd..60676e85 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -299,6 +299,27 @@ function integerBreak(n: number): number { }; ``` +### Rust + +```Rust +impl Solution { + fn max(a: i32, b: i32) -> i32{ + if a > b { a } else { b } + } + pub fn integer_break(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[2] = 1; + for i in 3..=n { + for j in 1..i - 1 { + dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32)); + } + } + dp[n] + } +} +``` + ### C ```c From b58c0df620f7e5171ec4606250ad7cdd04a3646c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9C=A1=E7=AC=94?= Date: Sun, 7 Aug 2022 20:41:01 +0800 Subject: [PATCH 7/9] =?UTF-8?q?Update=200376.=E6=91=86=E5=8A=A8=E5=BA=8F?= =?UTF-8?q?=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 按着讲解的原理,分别完善了Golang版本的贪心算法和动态规划算法。 --- problems/0376.摆动序列.md | 64 +++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index a41a0f0a..d15ed2d0 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -266,22 +266,58 @@ 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 + var count, preDiff, curDiff int //初始化默认为0 + count = 1 // 初始化为1,因为最小的序列是1个数 + if len(nums) < 2 { + return count + } + for i := 0; i < len(nums)-1; i++ { + curDiff = nums[i+1] - nums[i] + if (curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0) { + count++ + } + } + return count +} +``` + +**动态规划** +```golang +func wiggleMaxLength(nums []int) int { + n := len(nums) + if n <= 1 { + return n + } + dp := make([][2]int, n) + // i 0 作为波峰的最大长度 + // i 1 作为波谷的最大长度 + dp[0][0] = 1 + dp[0][1] = 1 + for i := 0; i < n; i++ { + for j := 0; j < i; j++ { + if nums[j] > nums[i] { //nums[i]为波谷 + dp[i][1] = max(dp[i][1], dp[j][0]+1) + } + if nums[j] < nums[i] { //nums[i]为波峰 或者相等 + dp[i][0] = max(dp[i][0], dp[j][1]+1) + } + if nums[j] == nums[i] { //添加一种情况,nums[i]为相等 + dp[i][0] = max(dp[i][0], dp[j][0]) //波峰 + dp[i][1] = max(dp[i][1], dp[j][1]) //波谷 + } + } + } + return max(dp[n-1][0], dp[n-1][1]) +} +func max(a, b int) int { + if a > b { + return a + } else { + return b + } } ``` From b6027889f5d04955091933474ccd5735b53e9f75 Mon Sep 17 00:00:00 2001 From: cezarbbb <105843128+cezarbbb@users.noreply.github.com> Date: Mon, 8 Aug 2022 16:25:40 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200096.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?=20Rust=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0096.不同的二叉搜索树 Rust版本 --- problems/0096.不同的二叉搜索树.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index 9adc0457..51de1e23 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -252,6 +252,24 @@ function numTrees(n: number): number { }; ``` +### Rust + +```Rust +impl Solution { + pub fn num_trees(n: i32) -> i32 { + let n = n as usize; + let mut dp = vec![0; n + 1]; + dp[0] = 1; + for i in 1..=n { + for j in 1..=i { + dp[i] += dp[j - 1] * dp[i - j]; + } + } + dp[n] + } +} +``` + ### C ```c From 736ae6a2076393b97555ed14fa299e8f656adc33 Mon Sep 17 00:00:00 2001 From: Jack_ZhijieFang <56966563+laerpeeK@users.noreply.github.com> Date: Thu, 18 Aug 2022 15:16:59 +0800 Subject: [PATCH 9/9] =?UTF-8?q?Update=200015.=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md=20typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update 0015.三数之和.md typescript版本。添加提前跳出循环判断条件。 --- problems/0015.三数之和.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index da319866..a4b6b84d 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -493,6 +493,9 @@ function threeSum(nums: number[]): number[][] { right: number = length - 1; let resArr: number[][] = []; for (let i = 0; i < length; i++) { + if (nums[i]>0) { + return resArr; //nums经过排序后,只要nums[i]>0, 此后的nums[i] + nums[left] + nums[right]均大于0,可以提前终止循环。 + } if (i > 0 && nums[i] === nums[i - 1]) { continue; }