Skip to content

Commit 429392a

Browse files
committed
feat: add javaScript solutions for lcof problems
1 parent 82a3f00 commit 429392a

File tree

150 files changed

+3854
-0
lines changed

Some content is hidden

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

150 files changed

+3854
-0
lines changed

lcof/面试题03. 数组中重复的数字/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ class Solution {
5959
}
6060
```
6161

62+
### JavaScript
63+
```js
64+
/**
65+
* @param {number[]} nums
66+
* @return {number}
67+
*/
68+
var findRepeatNumber = function(nums) {
69+
let m = {}
70+
for(let num of nums) {
71+
if(m[num]) return num
72+
m[num] = 1
73+
}
74+
};
75+
```
76+
6277
### ...
6378
```
6479
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findRepeatNumber = function(nums) {
6+
let m = {}
7+
for(let num of nums) {
8+
if(m[num]) return num
9+
m[num] = 1
10+
}
11+
};

lcof/面试题04. 二维数组中的查找/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ class Solution {
7373
}
7474
```
7575

76+
### JavaScript
77+
```js
78+
/**
79+
* @param {number[][]} matrix
80+
* @param {number} target
81+
* @return {boolean}
82+
*/
83+
var findNumberIn2DArray = function(matrix, target) {
84+
let row = matrix.length
85+
let col = matrix[0].length
86+
function dfs(i,j) {
87+
if(i < 0 || j >= col) {
88+
return false
89+
}
90+
if(matrix[i][j] === target) return true
91+
else if(matrix[i][j] > target) {
92+
return dfs(i-1,j)
93+
} else {
94+
return dfs(i,j+1)
95+
}
96+
}
97+
return dfs(row-1,0)
98+
};
99+
```
100+
76101
### ...
77102
```
78103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
var findNumberIn2DArray = function(matrix, target) {
7+
let row = matrix.length
8+
let col = matrix[0].length
9+
function dfs(i,j) {
10+
if(i < 0 || j >= col) {
11+
return false
12+
}
13+
if(matrix[i][j] === target) return true
14+
else if(matrix[i][j] > target) {
15+
return dfs(i-1,j)
16+
} else {
17+
return dfs(i,j+1)
18+
}
19+
}
20+
return dfs(row-1,0)
21+
};

lcof/面试题05. 替换空格/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ class Solution {
3333
}
3434
```
3535

36+
### JavaScript
37+
```js
38+
/**
39+
* @param {string} s
40+
* @return {string}
41+
*/
42+
var replaceSpace = function(s) {
43+
return s.split(' ').join('%20')
44+
};
45+
```
46+
3647
### ...
3748
```
3849
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var replaceSpace = function(s) {
6+
return s.split(' ').join('%20')
7+
};

lcof/面试题06. 从尾到头打印链表/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class Solution {
6060
}
6161
```
6262

63+
### JavaScript
64+
```js
65+
66+
```
67+
6368
### ...
6469
```
6570
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {number[]}
11+
*/
12+
var reversePrint = function(head) {
13+
let node = head
14+
let res = []
15+
while(node) {
16+
res.unshift(node.val)
17+
node = node.next
18+
}
19+
return res
20+
};

lcof/面试题07. 重建二叉树/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,43 @@ class Solution {
9999
}
100100
```
101101

102+
### JavaScript
103+
```js
104+
/**
105+
* Definition for a binary tree node.
106+
* function TreeNode(val) {
107+
* this.val = val;
108+
* this.left = this.right = null;
109+
* }
110+
*/
111+
/**
112+
* @param {number[]} preorder
113+
* @param {number[]} inorder
114+
* @return {TreeNode}
115+
*/
116+
var buildTree = function(preorder, inorder) {
117+
if(!preorder || !preorder.length) return null
118+
let preIdx = 0
119+
let inMap = {}
120+
for(let i=0;i<inorder.length;i++) {
121+
inMap[inorder[i]] = i
122+
}
123+
function func(start, end) {
124+
if(start > end) {
125+
return null
126+
}
127+
let preVal = preorder[preIdx]
128+
preIdx++
129+
let inIdx = inMap[preVal]
130+
let node = new TreeNode(preVal)
131+
node.left = func(start, inIdx - 1)
132+
node.right = func(inIdx + 1, end)
133+
return node
134+
}
135+
return func(0, preorder.length - 1)
136+
};
137+
```
138+
102139
### ...
103140
```
104141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {number[]} preorder
10+
* @param {number[]} inorder
11+
* @return {TreeNode}
12+
*/
13+
var buildTree = function(preorder, inorder) {
14+
if(!preorder || !preorder.length) return null
15+
let preIdx = 0
16+
let inMap = {}
17+
for(let i=0;i<inorder.length;i++) {
18+
inMap[inorder[i]] = i
19+
}
20+
function func(start, end) {
21+
if(start > end) {
22+
return null
23+
}
24+
let preVal = preorder[preIdx]
25+
preIdx++
26+
let inIdx = inMap[preVal]
27+
let node = new TreeNode(preVal)
28+
node.left = func(start, inIdx - 1)
29+
node.right = func(inIdx + 1, end)
30+
return node
31+
}
32+
return func(0, preorder.length - 1)
33+
};

lcof/面试题09. 用两个栈实现队列/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ class CQueue {
9292
*/
9393
```
9494

95+
### JavaScript
96+
```js
97+
var CQueue = function() {
98+
this.data = []
99+
this.helper = []
100+
};
101+
/**
102+
* @param {number} value
103+
* @return {void}
104+
*/
105+
CQueue.prototype.appendTail = function(value) {
106+
this.data.push(value)
107+
};
108+
/**
109+
* @return {number}
110+
*/
111+
CQueue.prototype.deleteHead = function() {
112+
if(this.data.length) {
113+
while(this.data.length > 1) {
114+
this.helper.push(this.data.pop())
115+
}
116+
let res = this.data.pop()
117+
while(this.helper.length) {
118+
this.data.push(this.helper.pop())
119+
}
120+
return res
121+
} else {
122+
return -1
123+
}
124+
};
125+
```
126+
95127
### ...
96128
```
97129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var CQueue = function() {
2+
this.data = []
3+
this.helper = []
4+
};
5+
/**
6+
* @param {number} value
7+
* @return {void}
8+
*/
9+
CQueue.prototype.appendTail = function(value) {
10+
this.data.push(value)
11+
};
12+
/**
13+
* @return {number}
14+
*/
15+
CQueue.prototype.deleteHead = function() {
16+
if(this.data.length) {
17+
while(this.data.length > 1) {
18+
this.helper.push(this.data.pop())
19+
}
20+
let res = this.data.pop()
21+
while(this.helper.length) {
22+
this.data.push(this.helper.pop())
23+
}
24+
return res
25+
} else {
26+
return -1
27+
}
28+
};

lcof/面试题10- I. 斐波那契数列/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ class Solution {
5858
}
5959
```
6060

61+
### JavaScript
62+
```js
63+
/**
64+
* @param {number} n
65+
* @return {number}
66+
*/
67+
var fib = function(n) {
68+
if(!n) return 0
69+
let pre = 0
70+
let cur = 1
71+
for(let i=2;i<=n;i++) {
72+
let c = (pre + cur)%(1e9+7)
73+
pre = cur
74+
cur = c
75+
}
76+
return cur
77+
};
78+
```
79+
6180
### ...
6281
```
6382
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var fib = function(n) {
6+
if(!n) return 0
7+
let pre = 0
8+
let cur = 1
9+
for(let i=2;i<=n;i++) {
10+
let c = (pre + cur)%(1e9+7)
11+
pre = cur
12+
cur = c
13+
}
14+
return cur
15+
};

lcof/面试题10- II. 青蛙跳台阶问题/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ class Solution {
5353
}
5454
```
5555

56+
### JavaScript
57+
```js
58+
/**
59+
* @param {number} n
60+
* @return {number}
61+
*/
62+
var numWays = function(n) {
63+
if(!n) return 1
64+
let pre = 1
65+
let cur = 1
66+
for(let i=2;i<=n;i++) {
67+
let c = (pre + cur) % (1e9 + 7)
68+
pre = cur
69+
cur = c
70+
}
71+
return cur
72+
};
73+
```
74+
5675
### ...
5776
```
5877
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var numWays = function(n) {
6+
if(!n) return 1
7+
let pre = 1
8+
let cur = 1
9+
for(let i=2;i<=n;i++) {
10+
let c = (pre + cur) % (1e9 + 7)
11+
pre = cur
12+
cur = c
13+
}
14+
return cur
15+
};

0 commit comments

Comments
 (0)