Skip to content

feat: add javaScript solutions for lcof problems #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lcof/面试题03. 数组中重复的数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ class Solution {
}
```

### JavaScript
```js
/**
* @param {number[]} nums
* @return {number}
*/
var findRepeatNumber = function(nums) {
let m = {}
for(let num of nums) {
if(m[num]) return num
m[num] = 1
}
};
```

### ...
```

Expand Down
11 changes: 11 additions & 0 deletions lcof/面试题03. 数组中重复的数字/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {number[]} nums
* @return {number}
*/
var findRepeatNumber = function(nums) {
let m = {}
for(let num of nums) {
if(m[num]) return num
m[num] = 1
}
};
25 changes: 25 additions & 0 deletions lcof/面试题04. 二维数组中的查找/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ class Solution {
}
```

### JavaScript
```js
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var findNumberIn2DArray = function(matrix, target) {
let row = matrix.length
let col = matrix[0].length
function dfs(i,j) {
if(i < 0 || j >= col) {
return false
}
if(matrix[i][j] === target) return true
else if(matrix[i][j] > target) {
return dfs(i-1,j)
} else {
return dfs(i,j+1)
}
}
return dfs(row-1,0)
};
```

### ...
```

Expand Down
21 changes: 21 additions & 0 deletions lcof/面试题04. 二维数组中的查找/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var findNumberIn2DArray = function(matrix, target) {
let row = matrix.length
let col = matrix[0].length
function dfs(i,j) {
if(i < 0 || j >= col) {
return false
}
if(matrix[i][j] === target) return true
else if(matrix[i][j] > target) {
return dfs(i-1,j)
} else {
return dfs(i,j+1)
}
}
return dfs(row-1,0)
};
11 changes: 11 additions & 0 deletions lcof/面试题05. 替换空格/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ class Solution {
}
```

### JavaScript
```js
/**
* @param {string} s
* @return {string}
*/
var replaceSpace = function(s) {
return s.split(' ').join('%20')
};
```

### ...
```

Expand Down
7 changes: 7 additions & 0 deletions lcof/面试题05. 替换空格/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {string} s
* @return {string}
*/
var replaceSpace = function(s) {
return s.split(' ').join('%20')
};
5 changes: 5 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Solution {
}
```

### JavaScript
```js

```

### ...
```

Expand Down
20 changes: 20 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reversePrint = function(head) {
let node = head
let res = []
while(node) {
res.unshift(node.val)
node = node.next
}
return res
};
37 changes: 37 additions & 0 deletions lcof/面试题07. 重建二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ class Solution {
}
```

### JavaScript
```js
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
if(!preorder || !preorder.length) return null
let preIdx = 0
let inMap = {}
for(let i=0;i<inorder.length;i++) {
inMap[inorder[i]] = i
}
function func(start, end) {
if(start > end) {
return null
}
let preVal = preorder[preIdx]
preIdx++
let inIdx = inMap[preVal]
let node = new TreeNode(preVal)
node.left = func(start, inIdx - 1)
node.right = func(inIdx + 1, end)
return node
}
return func(0, preorder.length - 1)
};
```

### ...
```

Expand Down
33 changes: 33 additions & 0 deletions lcof/面试题07. 重建二叉树/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function(preorder, inorder) {
if(!preorder || !preorder.length) return null
let preIdx = 0
let inMap = {}
for(let i=0;i<inorder.length;i++) {
inMap[inorder[i]] = i
}
function func(start, end) {
if(start > end) {
return null
}
let preVal = preorder[preIdx]
preIdx++
let inIdx = inMap[preVal]
let node = new TreeNode(preVal)
node.left = func(start, inIdx - 1)
node.right = func(inIdx + 1, end)
return node
}
return func(0, preorder.length - 1)
};
32 changes: 32 additions & 0 deletions lcof/面试题09. 用两个栈实现队列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ class CQueue {
*/
```

### JavaScript
```js
var CQueue = function() {
this.data = []
this.helper = []
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
this.data.push(value)
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
if(this.data.length) {
while(this.data.length > 1) {
this.helper.push(this.data.pop())
}
let res = this.data.pop()
while(this.helper.length) {
this.data.push(this.helper.pop())
}
return res
} else {
return -1
}
};
```

### ...
```

Expand Down
28 changes: 28 additions & 0 deletions lcof/面试题09. 用两个栈实现队列/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var CQueue = function() {
this.data = []
this.helper = []
};
/**
* @param {number} value
* @return {void}
*/
CQueue.prototype.appendTail = function(value) {
this.data.push(value)
};
/**
* @return {number}
*/
CQueue.prototype.deleteHead = function() {
if(this.data.length) {
while(this.data.length > 1) {
this.helper.push(this.data.pop())
}
let res = this.data.pop()
while(this.helper.length) {
this.data.push(this.helper.pop())
}
return res
} else {
return -1
}
};
19 changes: 19 additions & 0 deletions lcof/面试题10- I. 斐波那契数列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ class Solution {
}
```

### JavaScript
```js
/**
* @param {number} n
* @return {number}
*/
var fib = function(n) {
if(!n) return 0
let pre = 0
let cur = 1
for(let i=2;i<=n;i++) {
let c = (pre + cur)%(1e9+7)
pre = cur
cur = c
}
return cur
};
```

### ...
```

Expand Down
15 changes: 15 additions & 0 deletions lcof/面试题10- I. 斐波那契数列/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number} n
* @return {number}
*/
var fib = function(n) {
if(!n) return 0
let pre = 0
let cur = 1
for(let i=2;i<=n;i++) {
let c = (pre + cur)%(1e9+7)
pre = cur
cur = c
}
return cur
};
19 changes: 19 additions & 0 deletions lcof/面试题10- II. 青蛙跳台阶问题/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ class Solution {
}
```

### JavaScript
```js
/**
* @param {number} n
* @return {number}
*/
var numWays = function(n) {
if(!n) return 1
let pre = 1
let cur = 1
for(let i=2;i<=n;i++) {
let c = (pre + cur) % (1e9 + 7)
pre = cur
cur = c
}
return cur
};
```

### ...
```

Expand Down
15 changes: 15 additions & 0 deletions lcof/面试题10- II. 青蛙跳台阶问题/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number} n
* @return {number}
*/
var numWays = function(n) {
if(!n) return 1
let pre = 1
let cur = 1
for(let i=2;i<=n;i++) {
let c = (pre + cur) % (1e9 + 7)
pre = cur
cur = c
}
return cur
};
Loading