Skip to content

Commit 821b589

Browse files
author
hjzheng
committed
init
0 parents  commit 821b589

24 files changed

+854
-0
lines changed

1.两数之和.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @lc app=leetcode.cn id=1 lang=javascript
3+
*
4+
* [1] 两数之和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums
10+
* @param {number} target
11+
* @return {number[]}
12+
*/
13+
var twoSum = function(nums, target) {
14+
let map = new Map();
15+
16+
for (let i = 0; i < nums.length; i++) {
17+
if (map.has(nums[i])) {
18+
return [map.get(nums[i]), i];
19+
} else {
20+
map.set(target - nums[i], i);
21+
}
22+
}
23+
24+
return [];
25+
};
26+
// @lc code=end

102.二叉树的层次遍历.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode.cn id=102 lang=javascript
3+
*
4+
* [102] 二叉树的层次遍历
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {number[][]}
18+
*/
19+
var levelOrder = function(root) {
20+
21+
};
22+
// @lc code=end
23+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode.cn id=103 lang=javascript
3+
*
4+
* [103] 二叉树的锯齿形层次遍历
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {number[][]}
18+
*/
19+
var zigzagLevelOrder = function(root) {
20+
if (!root) return [];
21+
let q = [root],
22+
res = [];
23+
24+
while (q.length !== 0) {
25+
let size = q.length,
26+
currLevel = [];
27+
28+
for (let i = 0; i < size; i++) {
29+
let node = q.shift();
30+
(res.length & 1) === 1
31+
? currLevel.unshift(node.val)
32+
: currLevel.push(node.val);
33+
34+
if (node.left) q.push(node.left);
35+
if (node.right) q.push(node.right);
36+
}
37+
38+
res.push(currLevel);
39+
}
40+
41+
return res;
42+
};
43+
// @lc code=end

107.二叉树的层次遍历-ii.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=107 lang=javascript
3+
*
4+
* [107] 二叉树的层次遍历 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {number[][]}
18+
*/
19+
var levelOrderBottom = function(root) {
20+
if (!root) return [];
21+
let q = [root],
22+
res = [];
23+
24+
while (q.length !== 0) {
25+
let size = q.length,
26+
currLevel = [];
27+
28+
for (let i = 0; i < size; i++) {
29+
let node = q.shift();
30+
currLevel.push(node.val);
31+
if (node.left) q.push(node.left);
32+
if (node.right) q.push(node.right);
33+
}
34+
35+
res.unshift(currLevel);
36+
}
37+
38+
return res;
39+
};
40+
// @lc code=end

112.路径总和.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode.cn id=112 lang=javascript
3+
*
4+
* [112] 路径总和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @param {number} sum
18+
* @return {boolean}
19+
*/
20+
var hasPathSum = function(root, sum) {
21+
let res = false,
22+
tmp = [],
23+
tmpSum = 0;
24+
25+
function helper(root) {
26+
if (!root) return;
27+
28+
tmp.push(root.val);
29+
tmpSum += root.val;
30+
31+
if (!root.left && !root.right) {
32+
if (tmpSum === sum) {
33+
res = true;
34+
return;
35+
}
36+
}
37+
38+
helper(root.left);
39+
helper(root.right);
40+
41+
let val = tmp.pop();
42+
tmpSum -= val;
43+
}
44+
45+
helper(root);
46+
47+
return res;
48+
};
49+
// @lc code=end

113.路径总和-ii.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode.cn id=113 lang=javascript
3+
*
4+
* [113] 路径总和 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @param {number} sum
18+
* @return {number[][]}
19+
*/
20+
var pathSum = function(root, sum) {
21+
let res = [],
22+
tmp = [],
23+
tmpSum = 0;
24+
25+
function helper(root) {
26+
if (!root) return;
27+
28+
tmp.push(root.val);
29+
tmpSum += root.val;
30+
31+
if (!root.left && !root.right) {
32+
if (tmpSum === sum) {
33+
res.push([...tmp]);
34+
}
35+
}
36+
37+
helper(root.left);
38+
helper(root.right);
39+
40+
tmpSum -= tmp.pop();
41+
}
42+
43+
helper(root);
44+
45+
return res;
46+
};
47+
// @lc code=end

124.二叉树中的最大路径和.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode.cn id=124 lang=javascript
3+
*
4+
* [124] 二叉树中的最大路径和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {number}
18+
*/
19+
var maxPathSum = function(root) {
20+
let max = Number.MIN_SAFE_INTEGER;
21+
22+
function helper(root) {
23+
if (!root) return 0;
24+
25+
let left = Math.max(helper(root.left), 0);
26+
let right = Math.max(helper(root.right), 0);
27+
28+
max = Math.max(max, left + right + root.val);
29+
30+
return root.val + Math.max(left, right);
31+
}
32+
33+
helper(root);
34+
35+
return max;
36+
};
37+
// @lc code=end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=153 lang=javascript
3+
*
4+
* [153] 寻找旋转排序数组中的最小值
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums
10+
* @return {number}
11+
*/
12+
var findMin = function(nums) {
13+
let left = 0;
14+
let right = nums.length - 1;
15+
let mid = 0;
16+
while (left < right) {
17+
if (nums[left] < nums[right]) {
18+
return nums[left];
19+
}
20+
mid = parseInt((left + right) / 2);
21+
if (nums[mid] < nums[right]) {
22+
right = mid;
23+
} else {
24+
left = mid + 1;
25+
}
26+
}
27+
return nums[right];
28+
};
29+
// @lc code=end

155.最小栈.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @lc app=leetcode.cn id=155 lang=javascript
3+
*
4+
* [155] 最小栈
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* initialize your data structure here.
10+
*/
11+
var MinStack = function() {
12+
this.stack = [];
13+
this._minStack = [];
14+
};
15+
16+
/**
17+
* @param {number} x
18+
* @return {void}
19+
*/
20+
MinStack.prototype.push = function(x) {
21+
let minTop = this._minStack[this._minStack.length - 1];
22+
if (minTop === undefined || minTop >= x) {
23+
this._minStack.push(x);
24+
}
25+
this.stack.push(x);
26+
};
27+
28+
/**
29+
* @return {void}
30+
*/
31+
MinStack.prototype.pop = function() {
32+
let top = this.stack.pop();
33+
let minTop = this._minStack[this._minStack.length - 1];
34+
if (top === minTop) {
35+
this._minStack.pop();
36+
}
37+
};
38+
39+
/**
40+
* @return {number}
41+
*/
42+
MinStack.prototype.top = function() {
43+
return this.stack[this.stack.length - 1];
44+
};
45+
46+
/**
47+
* @return {number}
48+
*/
49+
MinStack.prototype.getMin = function() {
50+
return this._minStack[this._minStack.length - 1];
51+
};
52+
53+
/**
54+
* Your MinStack object will be instantiated and called as such:
55+
* var obj = new MinStack()
56+
* obj.push(x)
57+
* obj.pop()
58+
* var param_3 = obj.top()
59+
* var param_4 = obj.getMin()
60+
*/
61+
// @lc code=end

0 commit comments

Comments
 (0)