Skip to content

Commit ebda970

Browse files
authored
Merge pull request doocs#257 from matteokjh/feature/lcof_js
feat: add javaScript solutions for lcof problems
2 parents 82a3f00 + 429392a commit ebda970

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

Lines changed: 15 additions & 0 deletions
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
Lines changed: 11 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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
Lines changed: 21 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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
Lines changed: 7 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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
Lines changed: 20 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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
Lines changed: 33 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)