Skip to content

Commit fc602dd

Browse files
author
Chu Fan
committed
feat: 新增算法文档
1 parent 11ee1d7 commit fc602dd

File tree

11 files changed

+201
-171
lines changed

11 files changed

+201
-171
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* 难度:入门
44
* @param n
55
* @returns {*}
6-
* @constructor
76
*/
87
function fibonacciOne(n) {
98
return n < 2 ? n : fibonacciOne(n - 1) + fibonacciOne(n - 2)
@@ -21,9 +20,7 @@ function fibonacciTwo(n) {
2120

2221
let result = 1
2322
for (let index = 3; index <= n; index++) {
24-
// 求和
2523
result = firstValue + secondValue
26-
2724
// 前面两列重新赋值
2825
firstValue = secondValue
2926
secondValue = result

code/algorithm/剑指/双指针/FindNumbersWithSum.js

Lines changed: 0 additions & 66 deletions
This file was deleted.
File renamed without changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* @Description: 【中等】和为S的两个数字
3+
* @Version: Beta1.0
4+
* @Author: 【B站&公众号】Rong姐姐好可爱
5+
* @Date: 2021-04-30 20:29:08
6+
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
7+
* @LastEditTime: 2021-04-30 21:04:46
8+
*/
9+
10+
11+
/**
12+
* 利用双指针
13+
* @param array
14+
* @param sum
15+
* @returns {*[]}
16+
*/
17+
function findNumbersWithSumOne(array, sum) {
18+
let left = 0; let right = array.length - 1
19+
while (left < right) {
20+
if (array[left] + array[right] === sum) {
21+
// 第一个就返回
22+
return [array[left], array[right]]
23+
} else if (array[left] + array[right] > sum) {
24+
// 向左逼近
25+
right--
26+
} else {
27+
// 向右逼近
28+
left++
29+
}
30+
}
31+
return []
32+
}
33+
34+
/**
35+
* 注意数组array是递增的
36+
* 利用二分查找
37+
* @param array
38+
* @param sum
39+
* @returns {*[]}
40+
*/
41+
function findNumbersWithSumTwo(array, sum) {
42+
let left = 0
43+
let right = array.length - 1
44+
// 将最小值标记设置成最大
45+
let min = array[right] * array[right]
46+
let result = []
47+
while (left < right) {
48+
if (array[left] + array[right] === sum) {
49+
// 符合条件
50+
if (min > array[left] * array[right]) {
51+
// 最小值
52+
min = array[left] * array[right]
53+
result = [array[left], array[right]]
54+
}
55+
left++
56+
right--
57+
} else if (array[left] + array[right] < sum) {
58+
// 左边向右逼近
59+
left++
60+
} else {
61+
// 右边向左逼近
62+
right--
63+
}
64+
}
65+
return result
66+
}
67+
68+
/**
69+
* 利用map来存放数据,便于查找
70+
* @param array
71+
* @param sum
72+
*/
73+
function findNumbersWithSumThree(array, sum) {
74+
const resultMap = new Map()
75+
for (const a of array) {
76+
resultMap.set(a, true)
77+
}
78+
for (const a of array) {
79+
if (resultMap.has(sum - a)) {
80+
return [a, sum - a]
81+
}
82+
}
83+
return []
84+
}
85+
86+
/**
87+
* 暴力方案
88+
* @param array
89+
* @param sum
90+
*/
91+
function findNumbersWithSumFour(array, sum) {
92+
const len = array.length
93+
for (let index = 0; index < len; index++) {
94+
const firstValue = array[index]
95+
for (let j = index + 1; j < len; j++) {
96+
if (array[j] === sum - firstValue) {
97+
return [firstValue, array[j]]
98+
}
99+
}
100+
}
101+
return []
102+
}
103+
104+
console.log(findNumbersWithSumOne([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21))
105+
console.log(findNumbersWithSumTwo([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21))
106+
console.log(findNumbersWithSumThree([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21))
107+
console.log(findNumbersWithSumFour([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 21))
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
export const soloAlgorithmSidebar = [
2-
{
3-
text: '面试必须刷101',
4-
link: 'interview-101'
5-
},
6-
{
7-
text: '剑指Offer',
8-
link: 'sword-point'
9-
},
10-
{
11-
text: 'Shell篇',
12-
link: 'shell/'
13-
},
14-
{
15-
text: '在线刷题',
16-
children: [
17-
18-
{
19-
text: '牛客网',
20-
link: 'https://www.nowcoder.com/'
21-
},
22-
{
23-
text: 'LeetCode',
24-
link: 'https://leetcode-cn.com/'
25-
},
26-
{
27-
text: '杭电OJ',
28-
link: 'http://acm.hdu.edu.cn/'
29-
}
30-
]
31-
}
2+
{
3+
text: '面试必须刷101',
4+
link: 'interview-101'
5+
},
6+
{
7+
text: '剑指Offer',
8+
link: 'sword-point'
9+
},
10+
{
11+
text: 'Shell篇',
12+
link: 'shell'
13+
},
14+
{
15+
text: '在线刷题',
16+
children: [
17+
{
18+
text: '牛客网',
19+
link: 'https://www.nowcoder.com/'
20+
},
21+
{
22+
text: 'LeetCode',
23+
link: 'https://leetcode-cn.com/'
24+
},
25+
{
26+
text: '杭电OJ',
27+
link: 'http://acm.hdu.edu.cn/'
28+
}
29+
]
30+
}
3231
]

docs/manuscript/solo-algorithm/sword-point/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [x] [【简单】数组中重复的数字](数组和矩阵/duplicate.md)
1111
- [x] [【中等】二维数组中的查找](数组和矩阵/find.md)
1212
- [x] [【简单】替换空格](数组和矩阵/replaceSpace.md)
13-
- [ ] [【较难】顺时针打印矩阵](数组和矩阵/printMatrix.md)
13+
- [x] [【较难】顺时针打印矩阵](数组和矩阵/printMatrix.md)
1414
- [x] [【简单】第一个只出现一次的字符位置](数组和矩阵/firstNotRepeatingChar.md)
1515

1616

@@ -28,7 +28,7 @@
2828

2929
### 双指针
3030

31-
- [ ] [【中等】和为S的两个数字](双指针/findNumbersWithSum.md)
31+
- [x] [【中等】和为S的两个数字](双指针/findNumbersWithSum.md)
3232
- [ ] [【中等】和为S的连续正数序列](双指针/findContinuousSequence.md)
3333
- [ ] [【中等】左旋转字符串](双指针/leftRotateString.md)
3434
- [ ] [【较难】翻转单词顺序列](双指针/reverseSentence.md)

0 commit comments

Comments
 (0)