Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
su37josephxia committed Oct 12, 2023
1 parent cd74802 commit 6325556
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 04-algrithm/09-build-tree/index.js
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))
56 changes: 56 additions & 0 deletions 04-algrithm/10-spiral-text/index.js
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);
48 changes: 48 additions & 0 deletions 04-algrithm/10-spiral-text/index2.js
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));
50 changes: 50 additions & 0 deletions 04-algrithm/10-spiral-text/index3.js
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(' '));
})

0 comments on commit 6325556

Please sign in to comment.