Skip to content

Commit

Permalink
feat(algorithm): 算法文档更新,侧边栏优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Chu Fan committed Feb 23, 2023
1 parent b3be04f commit 80f08f6
Show file tree
Hide file tree
Showing 35 changed files with 251 additions and 505 deletions.
4 changes: 2 additions & 2 deletions code/algorithm/剑指/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
### 数组和矩阵

- [ ] [【简单】数组中重复的数字](./数组和矩阵/duplicate.js)
- [ ] [【中等】二维数组中的查找](./数组和矩阵/Find.js)
- [ ] [【中等】二维数组中的查找](./数组和矩阵/find.js)
- [ ] [【较难】替换空格](./数组和矩阵/replaceSpace.js)
- [ ] [【较难】顺时针打印矩阵](./数组和矩阵/printMatrix.js)
- [ ] [【简单】第一个只出现一次的字符位置](./数组和矩阵/FirstNotRepeatingChar.js)
- [ ] [【简单】第一个只出现一次的字符位置](./数组和矩阵/firstNotRepeatingChar.js)


### 栈队列堆
Expand Down
1 change: 0 additions & 1 deletion code/algorithm/剑指/位运算/FindNumsAppearOnce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
* @param array
* @returns {*[]}
*/
Expand Down
87 changes: 0 additions & 87 deletions code/algorithm/剑指/数组和矩阵/Find.js

This file was deleted.

110 changes: 110 additions & 0 deletions code/algorithm/剑指/数组和矩阵/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
*
* 每一行都按照从左到右递增的顺序排序
* 每一列都按照从上到下递增的顺序排序
*
* 直接循环找 选取右上或者左下元素为起点
*
*/

/**
* 右上角元素为起点,确保每个元素都能遍历到,都能按照方向走即可
* @param target
* @param array
*/
function findOne(target, array) {
// 行数
const row = array.length
if (row === 0) {
return false
}

// 列数
const col = array[0].length
// 此时选择右上为起点
let r = 0; let c = col - 1
while (r < row && c >= 0) {
if (target === array[r][c]) {
// 命中目标,返回
return true
}
// 目标元素比右上小,target往左找
if (target < array[r][c]) {
c--
}
// 目标元素比当前元素大,target往下找
if (target > array[r][c]) {
r++
}
}
return false
}

/**
* - 比较蠢的方法,通过遍历二维数组进行比较
* - 这里可以将第二层循环改成二分查找【用到从左到右递增的特点】;降低时间复杂度
* @param target
* @param array
* @returns {boolean}
*/
function findTwo(target, array) {
const row = array.length
if (row === 0) {
return false
}
// 列数
const col = array[0].length

// 从第0行开始
for (let r = 0; r < row; r++) {
let low = 0; let high = col - 1
// 注意这个二分查找区间是 []
while (low <= high) {
const mid = low + Math.floor((high - low) / 2)
if (array[r][mid] === target) {
return true
}
// 在左侧,从左边找
if (array[r][mid] > target) {
high = mid - 1
}
// 在右侧
if (array[r][mid] < target) {
low = mid + 1
}
}
}
return false
}

/**
* 利用一些api
* - every()是对数组中每一项运行给定函数,如果该函数对每一项返回true,则返回true
* - some()是对数组中每一项运行给定函数,如果该函数对任一项返回true,则返回true
* @param target
* @param array
*/
function findThree(target, array) {
return array.some(arr => arr.some(item => item === target))
}

console.log(findOne(19, [
[1, 2, 8, 9],
[2, 4, 9, 12],
[4, 7, 10, 13],
[6, 8, 11, 15]
]))

console.log(findTwo(19, [
[1, 2, 8, 9],
[2, 4, 9, 12],
[4, 7, 10, 13],
[6, 8, 11, 15]
]))

console.log(findThree(19, [
[1, 2, 8, 9],
[2, 4, 9, 12],
[4, 7, 10, 13],
[6, 8, 11, 15]
]))
16 changes: 12 additions & 4 deletions code/algorithm/剑指/树/reConstructBinaryTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@
* @LastEditTime: 2021-05-11 12:58:33
*/

/**
* 二叉树结点定义
* @param x
*/
function TreeNode(x) {
this.val = x
this.left = null
this.right = null
}

/**
* 重建二叉树
* @param pre
* @param vin
* @returns {TreeNode|null}
*/
function reConstructBinaryTree(pre, vin) {
if (pre.length === 0) {
return null
Expand All @@ -22,11 +33,8 @@ function reConstructBinaryTree(pre, vin) {
const rootIndex = vin.indexOf(pre[0])
console.log(rootIndex, pre[0], vin.slice(0, rootIndex), vin.slice(rootIndex + 1))
console.log(rootIndex, pre[0], pre.slice(1, rootIndex), pre.slice(rootIndex + 1))
// 注意,找中序结点的时候,需要去除根结点,先序的时候,
// 注意,找中序结点的时候,需要去除根结点,先序的时候,
rootNode.left = reConstructBinaryTree(pre.slice(1, rootIndex + 1), vin.slice(0, rootIndex))
rootNode.right = reConstructBinaryTree(pre.slice(rootIndex + 1), vin.slice(rootIndex + 1))

//

return rootNode
}
28 changes: 14 additions & 14 deletions docs/solo-algorithm/剑指/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

### 数组和矩阵

- [ ] [【简单】数组中重复的数字](./数组和矩阵/duplicate.md)
- [ ] [【中等】二维数组中的查找](./数组和矩阵/Find.md)
- [x] [【简单】数组中重复的数字](./数组和矩阵/duplicate.md)
- [x] [【中等】二维数组中的查找](./数组和矩阵/find.md)
- [ ] [【较难】替换空格](./数组和矩阵/replaceSpace.md)
- [ ] [【较难】顺时针打印矩阵](./数组和矩阵/printMatrix.md)
- [ ] [【简单】第一个只出现一次的字符位置](./数组和矩阵/FirstNotRepeatingChar.md)
- [ ] [【简单】第一个只出现一次的字符位置](./数组和矩阵/firstNotRepeatingChar.md)


### 栈队列堆

- [ ] [【简单】两个栈实现队列](./栈队列堆/JSStackToQueue.js)
- [ ] [【中等】最小的k个数](./栈队列堆/GetLeastNumbers_Solution.js)
- [ ] [【中等】数据流中的中位数](./栈队列堆/InsertAndGetMedian.js)
- [ ] [【中等】字符流中的第一个不重复的字符](./栈队列堆/FirstAppearingOnce.js)
- [ ] [【较难】滑动窗口的最大值](./栈队列堆/maxInWindows.js)
- [ ] [【较难】包含min函数的栈](./栈队列堆/GetMinInJSStack.js)
- [ ] [【简单】两个栈实现队列](./栈队列堆/stackToQueue.md)
- [ ] [【中等】最小的k个数](./栈队列堆/getLeastNumbersSolution.md)
- [ ] [【中等】数据流中的中位数](./栈队列堆/insertAndGetMedian.md)
- [ ] [【中等】字符流中的第一个不重复的字符](./栈队列堆/firstAppearingOnce.md)
- [ ] [【较难】滑动窗口的最大值](./栈队列堆/maxInWindows.md)
- [ ] [【较难】包含min函数的栈](./栈队列堆/getMinInJSStack.md)
- 栈的压入、弹出序列


Expand Down Expand Up @@ -52,11 +52,11 @@

###

- [ ] [【中等】重建二叉树](./树/reConstructBinaryTree.js)
- [ ] [【中等】二叉树的下一个结点](./树/GetNext.js)
- [ ] [【较难】树的子结构](./树/HasSubtree.js)
- [ ] [【简单】二叉树的镜像](./树/Mirror.js)
- [ ] [【困难】对称的二叉树](./树/isSymmetrical.js)
- [ ] [【中等】重建二叉树](./树/reConstructBinaryTree.md)
- [ ] [【中等】二叉树的下一个结点](./树/getNext.md)
- [ ] [【较难】树的子结构](./树/hasSubtree.md)
- [ ] [【简单】二叉树的镜像](./树/mirror.md)
- [ ] [【困难】对称的二叉树](./树/isSymmetrical.md)
- 从上往下打印二叉树
- 把二叉树打印成多行
- 二叉搜索树的后续遍历序列
Expand Down
87 changes: 0 additions & 87 deletions docs/solo-algorithm/剑指/数组和矩阵/Find.md

This file was deleted.

0 comments on commit 80f08f6

Please sign in to comment.