From 3947eeaab9d868fbe4665629a1792c1c9cd3a593 Mon Sep 17 00:00:00 2001 From: L1Y1 <108316255+L1Y1@users.noreply.github.com> Date: Thu, 13 Oct 2022 17:00:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 可以写的更通俗易懂一些 --- problems/0235.二叉搜索树的最近公共祖先.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 07e55ba5..a8c94ec9 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -328,13 +328,11 @@ var lowestCommonAncestor = function(root, p, q) { } if(root.val>p.val&&root.val>q.val) { // 向左子树查询 - let left = lowestCommonAncestor(root.left,p,q); - return left !== null&&left; + return root.left = lowestCommonAncestor(root.left,p,q); } if(root.val Date: Sun, 16 Oct 2022 20:34:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200216.=E7=BB=84=E5=90=88=E6=80=BB?= =?UTF-8?q?=E5=92=8CIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 感觉这种解法更符合卡哥视频的讲解。 --- problems/0216.组合总和III.md | 35 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 964facee..4273240c 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -397,24 +397,31 @@ func backTree(n,k,startIndex int,track *[]int,result *[][]int){ * @return {number[][]} */ var combinationSum3 = function(k, n) { - const backtrack = (start) => { - const l = path.length; - if (l === k) { - const sum = path.reduce((a, b) => a + b); - if (sum === n) { - res.push([...path]); - } - return; + let res = []; + let path = []; + let sum = 0; + const dfs = (path,index) => { + // 剪枝操作 + if (sum > n){ + return } - for (let i = start; i <= 9 - (k - l) + 1; i++) { + if (path.length == k) { + if(sum == n){ + res.push([...path]); + return + } + } + for (let i = index; i <= 9 - (k-path.length) + 1;i++) { path.push(i); - backtrack(i + 1); - path.pop(); + sum = sum + i; + index += 1; + dfs(path,index); + sum -= i + path.pop() } } - let res = [], path = []; - backtrack(1); - return res; + dfs(path,1); + return res }; ```