From 1acffe8867ef6699d059871972575fb15a30e4e3 Mon Sep 17 00:00:00 2001 From: Naxx-C <74910279+Naxx-C@users.noreply.github.com> Date: Fri, 14 May 2021 11:31:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index d9ecfc12..e975ef53 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -201,6 +201,21 @@ public: } return s; } + + /* 主函数简单写法 + string reverseWords(string s) { + removeExtraSpaces(s); + reverse(s, 0, s.size() - 1); + for(int i = 0; i < s.size(); i++) { + int j = i; + // 查找单词间的空格,翻转单词 + while(j < s.size() && s[j] != ' ') j++; + reverse(s, i, j - 1); + i = j; + } + return s; + } + */ }; ``` From 8eaba5884f9d499275c7c549eb4d846e8032fdc4 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Fri, 14 May 2021 15:03:44 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200051.N=E7=9A=87=E5=90=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0051.N皇后.md | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md index ac0235c2..935ea89c 100644 --- a/problems/0051.N皇后.md +++ b/problems/0051.N皇后.md @@ -363,7 +363,72 @@ Python: Go: +```Go +import "strings" +var res [][]string +func isValid(board [][]string, row, col int) (res bool){ + n := len(board) + for i:=0; i < row; i++ { + if board[i][col] == "Q" { + return false + } + } + for i := 0; i < n; i++{ + if board[row][i] == "Q" { + return false + } + } + + for i ,j := row, col; i >= 0 && j >=0 ; i, j = i - 1, j- 1{ + if board[i][j] == "Q"{ + return false + } + } + for i, j := row, col; i >=0 && j < n; i,j = i-1, j+1 { + if board[i][j] == "Q" { + return false + } + } + return true +} + +func backtrack(board [][]string, row int) { + size := len(board) + if row == size{ + temp := make([]string, size) + for i := 0; i Date: Fri, 14 May 2021 15:28:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A00106.=E4=BB=8E=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=20JavaSc?= =?UTF-8?q?ript=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...中序与后序遍历序列构造二叉树.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index e51cf08b..4cc2a88a 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -623,7 +623,21 @@ Python: Go: - +JavaScript +```javascript +var buildTree = function(inorder, postorder) { + if (!postorder.length) return null + + let root = new TreeNode(postorder[postorder.length - 1]) + + let index = inorder.findIndex(number => number === root.val) + + root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index)) + root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(index, postorder.length - 1)) + + return root +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)