Skip to content

Commit ff3fe03

Browse files
committed
手写api bind call apply科里化
1 parent cf204b0 commit ff3fe03

File tree

9 files changed

+178
-10
lines changed

9 files changed

+178
-10
lines changed

main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// import './树/Bst'
22

3-
import './链表/main'
3+
// import './链表/main'
44

55
// import './string/main'
66

7-
// import './动态规划/main'
7+
import './动态规划/main'
8+
9+
// import './数组/main'

动态规划/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { climbStairs, jump } from "./爬楼梯问题";
2+
import { cuttingRope, breakfastNumber } from './切绳子'
3+
4+
// console.log( jump(10), climbStairs(10) )
5+
6+
console.log(breakfastNumber([10,20,5],[5,5,2], 15))
27

3-
console.log( jump(10), climbStairs(10) )

动态规划/切绳子.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const cuttingRope = (n) => {
2+
const dp = [];
3+
dp[0] = dp[1] = dp[2] = 1;
4+
for (let i = 2; i <= n ; i++) {
5+
for (let j = i; j < i + 1; j++) {
6+
console.log( i , dp[i], j)
7+
dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]);
8+
}
9+
}
10+
return dp[n];
11+
}
12+
13+
/**
14+
* @param {number[]} staple
15+
* @param {number[]} drinks
16+
* @param {number} x
17+
* @return {number}
18+
*/
19+
export var breakfastNumber = function(staple, drinks, x) {
20+
let num = 0;
21+
for(let i = 0; i < staple.length; i ++){
22+
const stapleItem = staple[i];
23+
if(stapleItem > x) break
24+
for(let j = 0; j < drinks.length; j ++){
25+
const res = stapleItem + drinks[j];
26+
if(res <= x) num++
27+
}
28+
}
29+
return num % 1000000007
30+
};

动态规划/爬楼梯问题.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
* @return {number}
44
*/
55
export var climbStairs = function (n) {
6-
const dp = { '1': 1, "2": 2 }
7-
return (() => {
6+
const dp = { '1': 1, "2": 2 };
7+
const compox = (n) => {
88
if (dp[n]) {
9-
console.log(dp[n])
109
return dp[n]
1110
}
1211
if (n <= 0) return 0
13-
console.log(climbStairs(n - 1) + climbStairs(n - 2))
14-
dp[n] = climbStairs(n - 1) + climbStairs(n - 2)
15-
})()
12+
dp[n] = compox(n - 1) + compox(n - 2)
13+
return dp[n]
14+
}
15+
return compox(n)
1616
};
1717

1818
export function jump(n) {
19-
if (n <= 0) return -1;
19+
if (n <= 0) return 0;
2020
if (n == 1) return 1;
2121
if (n == 2) return 2;
2222
return jump(n - 1) + jump(n - 2);

数组/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**旋转数组最小值
2+
* @param {number[]} numbers
3+
* @return {number}
4+
*/
5+
var minArray = function (numbers) {
6+
let minValue = numbers[0];
7+
for(let i = 1; i <numbers.length; i ++){
8+
if(minValue > numbers[i]){
9+
return numbers[i]
10+
}
11+
}
12+
return i
13+
};
14+
15+
console.log( minArray([2,2,2,0,1] ))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Function.prototype.myCall = function (...conents) {
3+
const fn = this;
4+
const _self = conents.shift()
5+
_self.fn = fn;
6+
return _self.fn(...conents)
7+
}
8+
Function.prototype.myApply = function (...conents) {
9+
context.fn = this
10+
let result;
11+
// 判断是否有第二个参数
12+
if(conents[1]) {
13+
result = context.fn(...conents[1])
14+
} else {
15+
result = context.fn()
16+
}
17+
return result
18+
}
19+
20+
Function.prototype.myBind = function (_self) {
21+
_self.fn = this
22+
return (...arg) => {
23+
return _self.fn(...arg)
24+
}
25+
}
26+
const obj = {
27+
name: "obj"
28+
}
29+
30+
function test(params) {
31+
console.log(this.name)
32+
console.log(params)
33+
}
34+
35+
const newTest = test.bind(obj)
36+
const newTest2 = test.myBind(obj)
37+
newTest(111)
38+
newTest2(222)

笔试题/手写api/科里化.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function curry(fn, args = []) {
3+
var length = fn.length;
4+
var args = args || [];
5+
return function(...arg){
6+
const newArgs = [ ...args, ...arg];
7+
if (newArgs.length < length) {
8+
return curry(fn,newArgs);
9+
}else{
10+
return fn(...newArgs);
11+
}
12+
}
13+
}
14+
15+
16+
let curryTest = curry((a, b, c, d) => {
17+
return a+b+c+d
18+
},[2])
19+
// console.log( curryTest(1, 2, 3)(4) )//返回10
20+
console.log( curryTest(1, 2)(4) )//返回10
21+
// console.log( curryTest(1, 2)(3, 4) )//返回10

链表/leetcode160相交链表.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,25 @@ export var getIntersectionNode = function (headA, headB) {
1616
}
1717
return null
1818
};
19+
20+
/**
21+
* @param {ListNode} headA
22+
* @param {ListNode} headB
23+
* @return {ListNode}
24+
*/
25+
export var getIntersectionNode2 = function (headA, headB) {
26+
const visited = new Set();
27+
let temp = headA;
28+
while (temp !== null) {
29+
visited.add(temp);
30+
temp = temp.next;
31+
}
32+
temp = headB;
33+
while (temp !== null) {
34+
if (visited.has(temp)) {
35+
return temp;
36+
}
37+
temp = temp.next;
38+
}
39+
return null;
40+
};

队列/剑指offer06.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var CQueue = function () {
2+
this.stack1 = []
3+
this.stack2 = []
4+
};
5+
6+
/**
7+
* @param {number} value
8+
* @return {void}
9+
*/
10+
CQueue.prototype.appendTail = function (value) {
11+
this.stack1.push(value)
12+
};
13+
14+
/**
15+
* @return {number}
16+
*/
17+
CQueue.prototype.deleteHead = function () {
18+
// 如果第二个栈为空
19+
if (this.stack2.length === 0) {
20+
while (!this.stack1.length === 0) {
21+
this.stack2.push(this.stack1.pop());
22+
}
23+
}
24+
if (this.stack2.length === 0) {
25+
return -1;
26+
} else {
27+
return this.stack2.pop();
28+
}
29+
};
30+
31+
/**
32+
* Your CQueue object will be instantiated and called as such:
33+
* var obj = new CQueue()
34+
* obj.appendTail(value)
35+
* var param_2 = obj.deleteHead()
36+
*/

0 commit comments

Comments
 (0)