From 6325556918682db25ac22f58517440f3118435dd Mon Sep 17 00:00:00 2001 From: josephxia <15906475@qq.com> Date: Thu, 12 Oct 2023 15:38:22 +0800 Subject: [PATCH] update --- 04-algrithm/09-build-tree/index.js | 59 ++++++++++++++++++++++++++++ 04-algrithm/10-spiral-text/index.js | 56 ++++++++++++++++++++++++++ 04-algrithm/10-spiral-text/index2.js | 48 ++++++++++++++++++++++ 04-algrithm/10-spiral-text/index3.js | 50 +++++++++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 04-algrithm/09-build-tree/index.js create mode 100644 04-algrithm/10-spiral-text/index.js create mode 100644 04-algrithm/10-spiral-text/index2.js create mode 100644 04-algrithm/10-spiral-text/index3.js diff --git a/04-algrithm/09-build-tree/index.js b/04-algrithm/09-build-tree/index.js new file mode 100644 index 0000000..71962aa --- /dev/null +++ b/04-algrithm/09-build-tree/index.js @@ -0,0 +1,59 @@ +// 广度优先创建二叉树 +const node = { + v: 1, + l: { + v: 2, + }, + r: { + v: 3, + } +} + +const tree = { + +} +const max = 10 +const buildTree = (node, i) => { + if (i > max) return + node.v = i + node.l = buildTree({}, 2 * i) + node.r = buildTree({}, 2 * i + 1) + return node +} +buildTree(tree, 1) + + +// 递归中序遍历函数 +function inorderTraversal(root) { + const res = [] + const inorder = (root) => { + if (!root) { + return + } + res.push(root.v) + inorder(root.l) + inorder(root.r) + } + inorder(root) + return res +} +const res = inorderTraversal(tree) +console.log('中序遍历', res) + +function postorderTraversal(root) { + const res = [] + const inorder = (root) => { + if (!root) { + return + } + inorder(root.l) + inorder(root.r) + res.push(root.v) + } + inorder(root) + return res +} + +// console.log('后续序遍历', postorderTraversal(tree)) + +// console.log(JSON.stringify(tree, null, 4)) \ No newline at end of file diff --git a/04-algrithm/10-spiral-text/index.js b/04-algrithm/10-spiral-text/index.js new file mode 100644 index 0000000..63ddc9e --- /dev/null +++ b/04-algrithm/10-spiral-text/index.js @@ -0,0 +1,56 @@ +function spiralText(text) { + const rows = 5; // 矩阵的行数 + const cols = 5; // 矩阵的列数 + const matrix = new Array(rows); + for (let i = 0; i < rows; i++) { + matrix[i] = new Array(cols).fill(' '); + } + let direction = 0; // 初始方向(0表示向右,1表示向下,2表示向左,3表示向上) + let count = 0; // 用于记录已填写的字符数 + let row = 0; + let col = 0; + + for (let i = 0; i < text.length; i++) { + matrix[row][col] = text.charAt(i); + count++; + + if (direction === 0) { // 向右移动 + if (col < cols - 1 && matrix[row][col + 1] === ' ') { + col++; + } else { + direction = (direction + 1) % 4; + row++; + } + } else if (direction === 1) { // 向下移动 + if (row < rows - 1 && matrix[row + 1][col] === ' ') { + row++; + } else { + direction = (direction + 1) % 4; + col--; + } + } else if (direction === 2) { // 向左移动 + if (col > 0 && matrix[row][col - 1] === ' ') { + col--; + } else { + direction = (direction + 1) % 4; + row--; + } + } else { // 向上移动 + if (row > 0 && matrix[row - 1][col] === ' ') { + row--; + } else { + direction = (direction + 1) % 4; + col++; + } + } + } + + // 打印回型文字 + for (let i = 0; i < rows; i++) { + console.log(matrix[i].join(' ')); + } +} + +// 测试示例 +const text = "12345678901234567890"; +spiralText(text); diff --git a/04-algrithm/10-spiral-text/index2.js b/04-algrithm/10-spiral-text/index2.js new file mode 100644 index 0000000..509c6a1 --- /dev/null +++ b/04-algrithm/10-spiral-text/index2.js @@ -0,0 +1,48 @@ +const len = 5 +const arr = new Array(5).fill().map(() => new Array(5).fill(' ')); + + +const str = '123456789012345' +let direction = 0; // 0: right, 1: down, 2: left, 3: up +let row = 0; +let col = 0; +for (let i = 0; i < str.length; i++) { + arr[row][col] = str.charAt(i); + if (direction === 0) { + if (col < len - 1 && arr[row][col + 1] === ' ') { + col++; + } else { + direction = (direction + 1) % 4; + row++; + } + } else if (direction === 1) { + if (row < len - 1 && arr[row + 1][col] === ' ') { + row++; + } else { + direction = (direction + 1) % 4; + col--; + } + + } else if (direction === 2) { + if (col > 0 && arr[row][col - 1] === ' ') { + col--; + } else { + direction = (direction + 1) % 4; + row--; + } + } else { + if (row > 0 && arr[row - 1][col] === ' ') { + row--; + } else { + direction = (direction + 1) % 4; + col++; + } + } +} + +arr.forEach(item => { + console.log(item); +}) + + +// console.log(JSON.stringify(arr, null, 2)); \ No newline at end of file diff --git a/04-algrithm/10-spiral-text/index3.js b/04-algrithm/10-spiral-text/index3.js new file mode 100644 index 0000000..084cca7 --- /dev/null +++ b/04-algrithm/10-spiral-text/index3.js @@ -0,0 +1,50 @@ +const arr = new Array(5).fill().map(() => new Array(5).fill(' ')); + +const str = '1234567890123456789012345' +let direction = 0; // 0: right, 1: down, 2: left, 3: up +let row = 0; +let col = 0; + +for (i = 0; i < str.length; i++) { + arr[row][col] = str[i]; + if (direction === 0) { + if (col < 4 && arr[row][col + 1] === ' ') { + col++; + } + else { + direction = (direction + 1) % 4; + row++; + } + } else if (direction === 1) { + if (row < 4 && arr[row + 1][col] === ' ') { + row++; + } + else { + direction = (direction + 1) % 4; + col--; + } + } else if (direction === 2) { + if (col > 0 && arr[row][col - 1] === ' ') { + col--; + } + else { + direction = (direction + 1) % 4; + row--; + } + } else { + if (row > 0 && arr[row - 1][col] === ' ') { + row--; + } + else { + direction = (direction + 1) % 4; + col++; + } + } + + +} + + +arr.map(v => { + console.log(v.join(' ')); +}) \ No newline at end of file