-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd74802
commit 6325556
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(' ')); | ||
}) |