Skip to content

Commit 37be53d

Browse files
committed
add
1 parent 9d82e19 commit 37be53d

File tree

11 files changed

+430
-2
lines changed

11 files changed

+430
-2
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@
2828
"babel-jest": "^24.9.0",
2929
"jest": "^24.9.0",
3030
"webpack": "^4.41.2"
31+
},
32+
"dependencies": {
33+
"@types/jest": "^26.0.14",
34+
"ramda": "^0.27.1"
3135
}
3236
}

src/basic/recursive/fibonacciMemoization.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,58 @@ const fibonacciMemoization = function (n) {
88
}
99
return fibonacci(n);
1010
};
11-
console.log(fibonacciMemoization(100));
11+
console.log(fibonacciMemoization(3));
12+
13+
const fibonacciMemoization1 = function (n) {
14+
if (n <= 0) {
15+
return 0
16+
}
17+
const memo = [1, 1]
18+
if (n <= 2) {
19+
return memo[n - 1]
20+
}
21+
// 只需存储f(n-1)和f(n-2)的值
22+
for (let i = 3; i <= n; i++) {
23+
[memo[0], memo[1]] = [memo[1], memo[0] + memo[1]]
24+
}
25+
return memo[1]
26+
}
27+
28+
// [3,2,1,0,4]
29+
const jump = function (nums) {
30+
const len = nums.length
31+
let maxJumpLength = 0
32+
for (let i = 0; i < len; ++i) {
33+
if (i <= maxJumpLength) {
34+
maxJumpLength = Math.max(maxJumpLength, nums[i] + i)
35+
if (maxJumpLength >= len - 1) {
36+
return true
37+
}
38+
}
39+
}
40+
return false
41+
}
42+
// console.log(jump([1, 1]))
43+
44+
const canJump = function (nums) {
45+
// The "good" cell is a cell from which we may jump to the last cell of the nums array.
46+
47+
// The last cell in nums array is for sure the "good" one since it is our goal to reach.
48+
let leftGoodPosition = nums.length - 1;
49+
50+
// Go through all nums from right to left.
51+
for (let numberIndex = nums.length - 2; numberIndex >= 0; numberIndex -= 1) {
52+
// If we can reach the "good" cell from the current one then for sure the current
53+
// one is also "good". Since after all we'll be able to reach the end of the array
54+
// from it.
55+
const maxCurrentJumpLength = numberIndex + nums[numberIndex];
56+
if (maxCurrentJumpLength >= leftGoodPosition) {
57+
leftGoodPosition = numberIndex;
58+
}
59+
}
60+
61+
// If the most left "good" position is the zero's one then we may say that it IS
62+
// possible jump to the end of the array from the first cell;
63+
return leftGoodPosition === 0;
64+
}
65+
console.log(canJump([1, 1]));

src/basic/recursive/test.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
const coinList = [1,2,5];
2+
const count = 10;
3+
4+
/**
5+
*
6+
* 暴力
7+
*
8+
*
9+
function coinCount(coinList, count) {
10+
function dp(n) {
11+
if (n === 0) {
12+
return 0;
13+
}
14+
if (n < 0) {
15+
return -1;
16+
}
17+
let res = Number.MAX_SAFE_INTEGER;
18+
for (let i = 0; i < coinList.length; i++) {
19+
const sub = dp(n - coinList[i]);
20+
// 子问题无解
21+
if (sub < 0) {
22+
continue;
23+
}
24+
res = Math.min(res, 1 + sub);
25+
}
26+
return res;
27+
}
28+
return dp(count);
29+
}
30+
31+
console.log(coinCount(coinList, count));
32+
*/
33+
34+
35+
/*
36+
function coinCount(coinList, count) {
37+
// 带备忘录的递归
38+
const memo = {0:0};
39+
function dp(n) {
40+
if (memo.hasOwnProperty(n)) {
41+
return memo[n];
42+
}
43+
if (n === 0) {
44+
return 0;
45+
}
46+
if (n < 0) {
47+
return -1;
48+
}
49+
let res = Number.MAX_SAFE_INTEGER;
50+
for (let i = 0; i < coinList.length; i++) {
51+
const sub = dp(n - coinList[i]);
52+
// 子问题无解
53+
if (sub < 0) {
54+
continue;
55+
}
56+
res = Math.min(res, 1 + sub);
57+
}
58+
res !== Number.MAX_SAFE_INTEGER ? memo[n] = res : memo[n] = -1;
59+
return memo[n];
60+
}
61+
return dp(count);
62+
}
63+
64+
console.log(coinCount(coinList, count));
65+
*/
66+
67+
function coinCount(coinList, count) {
68+
// 初始化db数组
69+
// 凑成count,最多需要count个硬币(全部为1元的)
70+
const dp = Array(count+1).fill(count+1);
71+
dp[0] = 0;
72+
// i = 0,无效
73+
for (let i = 1; i < dp.length; i++) {
74+
for (let j = 0; j < coinList.length; j++) {
75+
if (i - coinList[j] < 0) {
76+
continue;
77+
}
78+
// Math.min当其中某个参数不能转换成number类型是,返回结果为NaN
79+
if(isNaN(dp[i])) {
80+
dp[i] = 1 + dp[i-coinList[j]];
81+
} else {
82+
dp[i] = Math.min(dp[i], 1 + dp[i-coinList[j]]);
83+
}
84+
}
85+
}
86+
return dp[count] === count + 1 ? -1 : dp[count];
87+
}
88+
89+
console.log(coinCount(coinList, count));
90+
91+
// 0 1 2 3 4
92+
// 1 4 3 4 2
93+
94+
// dp[3] = 3; ==> 1,3,4
95+
// dp[4] = 2; ==> 1,2
96+
// dp = [1, 2, 2, 3, 2] // 最长子序的长度
97+
const arr = [10,9,2,5,3,7,101,18];
98+
function maxIncrease(list) {
99+
// dp[i]代表以list[i]结尾的子序长度,而以list[i]结尾的字序必须包含list[i]这个元素,所以将db数组全部初始化为1,即:最小字序长度。
100+
const dp = Array(list.length).fill(1);
101+
for (let i = 0; i < list.length; i++) {
102+
for (let j = 0; j < i; j++) {
103+
// 以list[i]结尾的子序
104+
if (list[i] > list[j]) {
105+
dp[i] = Math.max(dp[i], dp[j] + 1);
106+
}
107+
}
108+
}
109+
let res = 0;
110+
for (let i = 0; i < dp.length; i++) {
111+
res = Math.max(res, dp[i]);
112+
}
113+
return res;
114+
}
115+
116+
// console.log(maxIncrease(arr));
117+
118+
// matrix[i][j]只能由matrix[i-1][j+1]、matrix[i-1][j]、matrix[i-1][j-1]
119+
const matrix = [[2, 1, 3], [6, 5, 4], [7, 8, 9]];
120+
/**
121+
*
122+
function dp(matrix, i, j) {
123+
if (i<0||j<0||i>=matrix.length||j>=matrix[0].length) {
124+
return Number.MAX_SAFE_INTEGER;
125+
}
126+
// 到了最顶层直接返回
127+
if (i === 0) {
128+
return matrix[i][j];
129+
}
130+
return matrix[i][j] + Math.min(dp(matrix, i-1, j-1), dp(matrix, i-1, j), dp(matrix, i-1, j+1));
131+
}
132+
133+
function minFallingPath(matrix) {
134+
let res = Number.MAX_SAFE_INTEGER;
135+
let len = matrix.length;
136+
137+
for (let i = 0; i < len; i++) {
138+
res = Math.min(res, dp(matrix, len-1, i));
139+
}
140+
return res;
141+
}
142+
143+
console.log('minFallingPath==> ', minFallingPath(matrix));
144+
*/
145+
146+
let memo;
147+
function dp(matrix, i, j) {
148+
if (i<0||j<0||i>=matrix.length||j>=matrix[0].length) {
149+
return Number.MAX_SAFE_INTEGER;
150+
}
151+
// 到了最顶层直接返回
152+
if (i === 0) {
153+
return matrix[0][j];
154+
}
155+
if (memo[i][j] !== Number.MAX_SAFE_INTEGER) {
156+
return memo[i][j];
157+
}
158+
memo[i][j] = matrix[i][j] + Math.min(dp(matrix, i-1, j-1), dp(matrix, i-1, j), dp(matrix, i-1, j+1));
159+
return memo[i][j];
160+
}
161+
162+
function minFallingPath(matrix) {
163+
let res = Number.MAX_SAFE_INTEGER;
164+
let len = matrix.length;
165+
memo = Array(len).fill(Array(len).fill(Number.MAX_SAFE_INTEGER));
166+
for (let i = 0; i < len; i++) {
167+
res = Math.min(res, dp(matrix, len-1, i));
168+
}
169+
return res;
170+
}
171+
172+
console.log('minFallingPath==> ', minFallingPath(matrix));

src/basic/regexp/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* match position
3+
*/
4+
5+
// var result = "I\nlove\njavascript".replace(/^|$/mg, '♥');
6+
// console.log(result);
7+
8+
// 单词边界
9+
// const result = "[JS] Lesson_01.mp4".replace(/\b/g, '#');
10+
// console.log(result);
11+
12+
13+
/**
14+
* (?=p)和(?!p) => "p"是一个子模式
15+
* (?=p):表示匹配"p"前面的位置
16+
* (?!p):表示与(?=p)的结果相反,不匹配"p"的前面位置(其他位置都匹配)
17+
*
18+
*/
19+
// const result = "hello".replace(/(?=l)/g, '♥');
20+
// console.log(result);
21+
const result = "hello".replace(/(?!o)/g, "♥");
22+
console.log(result);

src/basic/regexp/less-if.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const R = require('ramda')
2+
3+
const fn = R.cond([
4+
[R.equals(0), R.always('water freezes at 0°C')],
5+
[R.equals(100), R.always('water boils at 100°C')],
6+
[R.T, (temp) => `nothing special happends at ${temp}°C`]
7+
])
8+
9+
console.log(fn(0), fn(20), fn(100))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function letterCasePermutation(S) {
2+
const res = []
3+
const backtrack = (str, i) => {
4+
if (i === S.length) {
5+
return res.push(str)
6+
}
7+
const cur = S[i]
8+
// 对于字母
9+
if ((cur >= 'A' && cur <= 'Z') || (cur >= 'a' && cur <= 'z')) {
10+
const low = str + cur.toLowerCase(),
11+
upper = str + cur.toUpperCase()
12+
backtrack(low, i + 1)
13+
backtrack(upper, i + 1)
14+
} else {
15+
// 对于数字
16+
backtrack(str + cur, i + 1)
17+
}
18+
}
19+
backtrack('', 0)
20+
return res
21+
}
22+
23+
console.log(letterCasePermutation('a1b2'))

src/basic/regexp/lookAhead.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function lookAhead (str) {
2+
// const reg = /\d+(?=€)/
3+
// const reg = /(?<![-\d])\d+/g
4+
// const reg =  /(?!\b)(?=(\d{3})+\b)/g
5+
const reg = /(?<quote>['"])(.*?)\k<quote>/g
6+
7+
return str.match(reg)
8+
}

src/basic/regexp/lookAhead.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {lookAhead} from './lookAhead'
2+
3+
describe('test lookAhead',() => {
4+
it('search amount', ()=> {
5+
// const str = '1 turkey costs 30€'
6+
// const str = '0 12 -5 123 -18'
7+
// /(?=(\d{3})+\b)/g
8+
// const str = '517654321'
9+
const str = `He said: "She's the one!".`
10+
const expectResult = "She's the one!"
11+
expect(lookAhead(str)).toEqual(expectResult)
12+
})
13+
})

src/basic/regexp/lookBehind.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function lookBehind (str) {
2+
const reg = /(?<=(\$|£))\d+/
3+
return str.match(reg)
4+
}

src/basic/regexp/lookBehind.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {lookBehind} from './lookBehind'
2+
3+
describe('test lookBehind', () => {
4+
it('search amount', () => {
5+
const str = '衬衫的价格是:$300,所以你选择B答案!'
6+
const expectResult = '$300'
7+
expect(lookBehind(str)).toEqual(expectResult)
8+
})
9+
})

0 commit comments

Comments
 (0)