From 7851e51c7345590977d3881424354d6be0dd8eae Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:35:27 +0800 Subject: [PATCH 01/14] =?UTF-8?q?update=20=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 9c6d481d..3f222a17 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -218,6 +218,10 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + + 还记得我们在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中给出的回溯法模板么? From e533cb9cd4a5048a933055d5dd02ad7dbf8a1d20 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:37:21 +0800 Subject: [PATCH 02/14] =?UTF-8?q?update=20=20=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合优化.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 9736549c..0c816bc1 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -130,6 +130,10 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + + # 总结 From 9a29e5c3be75185c6013aaa0cb252374fb80598d Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 15:41:40 +0800 Subject: [PATCH 03/14] =?UTF-8?q?update=20=20=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CIII=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index f631c3cd..f08d77ea 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -235,6 +235,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) # 总结 From 025854e1678ba1e77a271b35a60159c1ad92ffbb Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:02:29 +0800 Subject: [PATCH 04/14] =?UTF-8?q?update=20=20=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index d1135497..d506bb88 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -183,6 +183,8 @@ public: } }; ``` +* 时间复杂度: O(3^m * 4^n),其中 m 是对应四个字母的数字个数,n 是对应三个字母的数字个数 +* 空间复杂度: O(3^m * 4^n) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) From d3290c7e773f66514625e8d678ff7695ae3f0f99 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:07:21 +0800 Subject: [PATCH 05/14] =?UTF-8?q?update=20=20=200039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index c4ee5ca6..e1e51923 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -214,6 +214,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n),注意这只是复杂度的上界,因为剪枝的存在,真实的时间复杂度远小于此 +* 空间复杂度: O(target) # 总结 From d68a1f633e532621ebd87578ae7c8daae4e5c36c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:08:26 +0800 Subject: [PATCH 06/14] =?UTF-8?q?update=20=20=200040.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CII=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0040.组合总和II.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 83708df7..b708650a 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -214,6 +214,8 @@ public: }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) ## 补充 From 65f059ac7a7168a8848bb7106f1050ce28afa862 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:27:02 +0800 Subject: [PATCH 07/14] =?UTF-8?q?update=20=200131.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E4=B8=B2=20=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index 30bba455..dfec7853 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -209,6 +209,9 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n^2) + # 优化 上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在: From 33d5a6878e73800cae191d98276729c1ebb77860 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:33:52 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=20update=20=20=200078.=E5=AD=90=E9=9B=86?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0078.子集.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0078.子集.md b/problems/0078.子集.md index f26d0821..07418fbc 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -149,6 +149,8 @@ public: }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) 在注释中,可以发现可以不写终止条件,因为本来我们就要遍历整棵树。 From 3accc7602597a2d6c7af017326d8caf9cb30be16 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:34:55 +0800 Subject: [PATCH 09/14] =?UTF-8?q?update=20=20=200090.=E5=AD=90=E9=9B=86II?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0090.子集II.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 1a9f8fda..63f75d29 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -83,6 +83,9 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) + 使用set去重的版本。 ```CPP From 09975d3d380c3151538270fe8cac25d39a476281 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:37:53 +0800 Subject: [PATCH 10/14] =?UTF-8?q?update=20=20=200491.=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0491.递增子序列.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 436dbf01..d6c6b9c9 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -139,6 +139,8 @@ public: } }; ``` +* 时间复杂度: O(n * 2^n) +* 空间复杂度: O(n) ## 优化 From 9d581e86fe858577263597ec5b0a1aa5dda24c2e Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:38:59 +0800 Subject: [PATCH 11/14] =?UTF-8?q?update=20=200046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index e08aec94..fb70be41 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -136,6 +136,8 @@ public: } }; ``` +* 时间复杂度: O(n!) +* 空间复杂度: O(n) ## 总结 From 1a9cb629ae87aa43a7eac4dea0a6732492a8a242 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:39:37 +0800 Subject: [PATCH 12/14] =?UTF-8?q?update=20=200047.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97II=20=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0047.全排列II.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md index b1908fb4..3ff3fb8f 100644 --- a/problems/0047.全排列II.md +++ b/problems/0047.全排列II.md @@ -99,6 +99,8 @@ public: }; ``` +* 时间复杂度: O(n) +* 空间复杂度: O(n) ## 拓展 From 2a7f4d5d4b4107c0aa85960e0e46adb56d665def Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 16:44:28 +0800 Subject: [PATCH 13/14] =?UTF-8?q?update=20=20=200051.N=E7=9A=87=E5=90=8E?= =?UTF-8?q?=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82=E5=BA=A6=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index bd1d1c9b..54580cf7 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -208,6 +208,9 @@ public: } }; ``` +* 时间复杂度: O(n!) +* 空间复杂度: O(n) + 可以看出,除了验证棋盘合法性的代码,省下来部分就是按照回溯法模板来的。 From 7f699b59167263645112138b8ff4a99e8c606846 Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Thu, 27 Apr 2023 17:15:12 +0800 Subject: [PATCH 14/14] =?UTF-8?q?update=20=20=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0093.复原IP地址.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 9d4d5918..161fb96e 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -244,6 +244,8 @@ public: }; ``` +* 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。 +* 空间复杂度: O(n) # 总结