Skip to content

Commit 74e8483

Browse files
author
Chu Fan
committed
docs(algorithm): 新增一些文档
- 算法思路整理 - 优化代码 - 更新eslint
1 parent b283402 commit 74e8483

41 files changed

Lines changed: 451 additions & 289 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

code/algorithm/剑指/位运算/FindNumsAppearOnce.js

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* 先排序,利用出现一次特性
3+
* @param array
4+
* @returns {*[]}
5+
*/
6+
function findNumsAppearOnceOne(array) {
7+
// 数组中元素要么出现一次,要么出现两次,可以先对元素进行排序有点偷懒的样子
8+
array = array.sort((a, b) => a - b)
9+
const len = array.length
10+
// 此时的 数组已经进过排序
11+
const onceResult = []
12+
13+
// 在开头
14+
if (array[0] !== array[1]) {
15+
onceResult.push(array[0])
16+
}
17+
// 在中间
18+
for (let index = 1; index < len - 1; index++) {
19+
if (array[index - 1] !== array[index] && array[index] !== array[index + 1]) {
20+
onceResult.push(array[index])
21+
}
22+
}
23+
// 在结尾
24+
if (array[array.length - 1] !== array[array.length - 2]) {
25+
onceResult.push(array[array.length - 1])
26+
}
27+
return onceResult
28+
}
29+
30+
/**
31+
* 利用Map统计出现次数
32+
* @param array
33+
* @returns {*[]}
34+
*/
35+
function findNumsAppearOnceTwo(array) {
36+
const resMap = new Map()
37+
38+
// 统计
39+
for (const value of array) {
40+
if (resMap.has(value)) {
41+
resMap.set(value, resMap.get(value) + 1)
42+
} else {
43+
resMap.set(value, 1)
44+
}
45+
}
46+
47+
const onceResult = []
48+
// 找出出现一次的
49+
for (const [key, value] of resMap) {
50+
if (value === 1) {
51+
onceResult.push(key)
52+
}
53+
}
54+
// 按从小到大输出
55+
return onceResult.sort((a, b) => a - b)
56+
}
57+
58+
/**
59+
* 利用异或运算
60+
* @param array
61+
* @returns {*[]}
62+
*/
63+
function findNumsAppearOnceThree(array) {
64+
let res1 = 0
65+
let res2 = 0
66+
let temp = 0
67+
// 遍历数组得到a^b
68+
for (let i = 0; i < array.length; i++) { temp ^= array[i] }
69+
let k = 1
70+
71+
// 找到两个数不相同的第一位
72+
while ((k & temp) === 0) { k <<= 1 }
73+
for (let i = 0; i < array.length; i++) {
74+
// 遍历数组,对每个数分类
75+
if ((k & array[i]) === 0) { res1 ^= array[i] } else { res2 ^= array[i] }
76+
}
77+
78+
// 升序
79+
return res1 > res2 ? [res2, res1] : [res1, res2]
80+
}
81+
82+
console.log(findNumsAppearOnceOne([1, 2, 3, 3, 2, 9]))
83+
console.log(findNumsAppearOnceTwo([1, 2, 3, 3, 2, 9]))
84+
console.log(findNumsAppearOnceThree([1, 2, 3, 3, 2, 9]))

code/algorithm/剑指/动态规划/Fibonacci.js

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 斐波那契数列,递归调用
3+
* 难度:入门
4+
* @param n
5+
* @returns {*}
6+
* @constructor
7+
*/
8+
function fibonacciOne(n) {
9+
return n < 2 ? n : fibonacciOne(n - 1) + fibonacciOne(n - 2)
10+
}
11+
12+
/**
13+
* 斐波那契数列,迭代
14+
* 难度:入门
15+
* @param n
16+
*/
17+
function fibonacciTwo(n) {
18+
// 数列初始化
19+
let firstValue = 0
20+
let secondValue = 1
21+
22+
let result = 1
23+
for (let index = 3; index <= n; index++) {
24+
// 求和
25+
result = firstValue + secondValue
26+
27+
// 前面两列重新赋值
28+
firstValue = secondValue
29+
secondValue = result
30+
}
31+
return result
32+
}
33+
34+
35+
console.log(fibonacciOne(4))
36+
console.log(fibonacciTwo(4))
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11

22
/**
3-
* 【中等】跳台阶 递归,要么跳一阶,要么跳两阶
3+
* 【简单】跳台阶 递归,要么跳一阶,要么跳两阶
44
* 思路: 对于第number台阶,只能从第number-1或者number-2上跳上来
55
* 记作: G(number)=G(number-1)+G(number-2)
66
* 即: 从第number-1跳上来的次数+从第number-2上跳上来的次数
77
*
88
*/
99

10-
// 递归实现
11-
function jumpFloor(number) {
10+
/**
11+
* 递归实现
12+
* @param number
13+
* @returns {*}
14+
*/
15+
function jumpFloorOne(number) {
1216
// 递归,要么跳一阶,要么跳两阶
13-
return number < 3 ? number : jumpFloor(number - 1) + jumpFloor(number - 2)
17+
return number < 3 ? number : jumpFloorOne(number - 1) + jumpFloorOne(number - 2)
1418
}
1519

16-
// 非递归调用
17-
function jumpFloor01(number) {
20+
/**
21+
* 非递归调用
22+
* @param number
23+
* @returns {number|*}
24+
*/
25+
function jumpFloorTwo(number) {
1826
let a = 1
1927
let b = 2
20-
let result = 0
21-
22-
if (number < 3) {
23-
return number
24-
}
28+
let result = 1
2529

2630
for (let index = 3; index <= number; index++) {
2731
result = a + b
2832
a = b
2933
b = result
3034
}
31-
3235
return result
3336
}
37+
38+
console.log(jumpFloorOne(7))
39+
console.log(jumpFloorTwo(7))
40+
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
function jumpFloorII(number) {
2-
// 我tm跳1 还剩下n-1阶 记作 G(n-1) 可选
3-
// 我tm跳2 还剩下n-2阶 记作 G(n-2) 可选
4-
// ....
5-
// 我tm跳n-1 还剩下1阶 记作 G(1) 可选
6-
7-
// 归纳出: G(n-1)=G(1)+G(2)+.....+G(n-2);
8-
// G(n)=G(1)+G(2)+.....+G(n-2)+G(n-1)
1+
/**
2+
* 利用Math函数计算幂
3+
* @param number
4+
* @returns {number}
5+
*/
6+
function jumpFloorIIOne(number) {
7+
return Math.pow(2, number - 1)
8+
}
99

10-
// 两个相减 G(n)=2* G(n-1) 去递归 G(1)=1 , G(2)=2 G(3)=2*G(2)=4
1110

12-
// 开始代码
13-
// return Math.pow(2,number-1)
14-
// 位运算
11+
/**
12+
* 利用左移运算
13+
* @param number
14+
* @returns {number}
15+
*/
16+
function jumpFloorIITwo(number) {
1517
// return 1<<(number-1)
1618
return 1 << --number
1719
}
20+
21+
22+
console.log(jumpFloorIIOne(2))
23+
console.log(jumpFloorIITwo(4))
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
2-
## 刷题整理
1+
# SOLO算法
32

43

54
### 牛客网
65

76
面试必刷TOP101:<https://www.nowcoder.com/exam/oj>
87

9-
#### 链表
10-
11-
- 反转链表
12-
13-
- 链表内指定区间反转
148

9+
### 算法模型
1510

16-
#### 二分查找/排序
11+
#### 快速排序
1712

13+
#### 堆排序
1814

19-
#### 二叉树
2015

21-
#### 堆/栈/队列
16+
### 面试必刷101
2217

23-
#### 哈希
2418

25-
#### 递归/回溯
19+
### 剑指Offer
2620

27-
#### 动态规划
2821

2922

30-
#### 字符串
23+
### 参考资料
3124

32-
## 剑指Offer
25+
- <https://github.com/jiajunhua/labuladong-fucking-algorithm>
3326

0 commit comments

Comments
 (0)