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) 一些写法,是把回溯的过程放在递归函数里了,例如如下代码,我可以写成这样:(注意注释中不一样的地方) 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) # 总结 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) ## 补充 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) ## 总结 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) ## 拓展 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) + 可以看出,除了验证棋盘合法性的代码,省下来部分就是按照回溯法模板来的。 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)中给出的回溯法模板么? 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) + + # 总结 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) 在注释中,可以发现可以不写终止条件,因为本来我们就要遍历整棵树。 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 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) # 总结 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```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在: 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) # 总结 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) ## 优化