Skip to content

Commit 41e9182

Browse files
committed
📖 add 200
1 parent 139b98c commit 41e9182

19 files changed

+589
-4
lines changed

101.对称二叉树.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=101 lang=javascript
3+
*
4+
* [101] 对称二叉树
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 {boolean}
18+
*/
19+
var isSymmetric = function(root) {
20+
return helper(root, root)
21+
};
22+
23+
const helper = function(p, q) {
24+
if (p == null && q == null)
25+
return true
26+
if (p == null || q == null)
27+
return false
28+
return p.val == q.val && helper(p.left, q.right) && helper(p.right, q.left)
29+
}
30+
// @lc code=end
31+

104.二叉树的最大深度.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=104 lang=javascript
3+
*
4+
* [104] 二叉树的最大深度
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 maxDepth = function(root) {
20+
if(root === undefined || root===null){
21+
return 0;
22+
}
23+
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
24+
};
25+
// @lc code=end
26+

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
let h = height(root); /* get the height using helper fn below */
21+
let results = []; /* init an array to hold final answer */
22+
let depth = 1; /* init depth to 1 since we are using 1-based height */
23+
for (depth; depth <= h; depth++) {
24+
/* iterate from depth to tree height */
25+
results.push(levelOrder(root, depth)); /* push an array of each level's node values to results */
26+
}
27+
28+
return results.reverse();
29+
};
30+
31+
/*
32+
recursive level-order traversal which collects all nodes
33+
from each level into an array.
34+
*/
35+
function levelOrder(node, d, level = []) {
36+
if (node == null) {
37+
return level;
38+
}
39+
if (d === 1) {
40+
level.push(node.val);
41+
} else if (d > 1) {
42+
levelOrder(node.left, d - 1, level);
43+
levelOrder(node.right, d - 1, level);
44+
}
45+
return level;
46+
}
47+
48+
/*
49+
computes the largest root-to-leaf path,
50+
using 1-based counting.
51+
*/
52+
function height(node) {
53+
if (node == null) {
54+
/* base case */
55+
return 0;
56+
}
57+
let left = height(node.left);
58+
let right = height(node.right);
59+
60+
return Math.max(left, right) + 1;
61+
}
62+
// @lc code=end
63+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode.cn id=108 lang=javascript
3+
*
4+
* [108] 将有序数组转换为二叉搜索树
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 {number[]} nums
17+
* @return {TreeNode}
18+
*/
19+
var sortedArrayToBST = function(nums) {
20+
if (!nums.length) return null;
21+
22+
const mid = Math.floor(nums.length / 2);
23+
const root = new TreeNode(nums[mid]);
24+
25+
// subtrees are BSTs as well
26+
root.left = sortedArrayToBST(nums.slice(0, mid));
27+
root.right = sortedArrayToBST(nums.slice(mid + 1));
28+
29+
return root;
30+
};
31+
// @lc code=end
32+

110.平衡二叉树.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=110 lang=javascript
3+
*
4+
* [110] 平衡二叉树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {TreeNode} root
18+
* @return {boolean}
19+
*/
20+
var isBalanced = function(root) {
21+
22+
let dfs = function(node) {
23+
if (!node) return 0;
24+
let left = 1 + dfs(node.left);
25+
let right = 1 + dfs(node.right);
26+
if (Math.abs(left - right) > 1) return Infinity;
27+
return Math.max(left, right);
28+
}
29+
30+
return dfs(root)==Infinity?false:true;
31+
};
32+
// @lc code=end
33+

111.二叉树的最小深度.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode.cn id=111 lang=javascript
3+
*
4+
* [111] 二叉树的最小深度
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {TreeNode} root
18+
* @return {number}
19+
*/
20+
var minDepth = function(root) {
21+
if (root === null) return 0;
22+
if (root.left === null) return minDepth(root.right) + 1;
23+
if (root.right === null) return minDepth(root.left) + 1;
24+
return Math.min( minDepth(root.left), minDepth(root.right) ) + 1;
25+
};
26+
// @lc code=end
27+

112.路径总和.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
if (!root) return false;
22+
23+
if (!root.left && !root.right) { // check leaf
24+
return sum === root.val;
25+
} else { // continue DFS
26+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
27+
}
28+
};
29+
// @lc code=end
30+

118.杨辉三角.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=118 lang=javascript
3+
*
4+
* [118] 杨辉三角
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} numRows
10+
* @return {number[][]}
11+
*/
12+
var generate = function(numRows) {
13+
var pascal = [];
14+
for (var i = 0; i < numRows; i++) {
15+
pascal[i] = [];
16+
pascal[i][0] = 1;
17+
for (var j = 1; j < i; j++) {
18+
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j]
19+
}
20+
pascal[i][i] = 1;
21+
}
22+
return pascal;
23+
};
24+
// @lc code=end
25+

119.杨辉三角-ii.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=119 lang=javascript
3+
*
4+
* [119] 杨辉三角 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} rowIndex
10+
* @return {number[]}
11+
*/
12+
var getRow = function(rowIndex) {
13+
var row = [1];
14+
15+
for(var i = 1 ; i <= rowIndex ; i++) {
16+
for(var j = i; j > 0; j--) {
17+
if(j === i)
18+
row[j] = 1;
19+
else
20+
row[j] = row[j - 1] + row[j];
21+
}
22+
}
23+
return row;
24+
};
25+
// @lc code=end
26+

122.买卖股票的最佳时机-ii.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode.cn id=122 lang=javascript
3+
*
4+
* [122] 买卖股票的最佳时机 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} prices
10+
* @return {number}
11+
*/
12+
var maxProfit = function(prices) {
13+
let profit = 0;
14+
15+
for (let i = 1; i < prices.length; i++) {
16+
let prev = prices[i - 1];
17+
let current = prices[i];
18+
19+
if (prev < current) {
20+
profit += current - prev;
21+
}
22+
}
23+
24+
return profit;
25+
};
26+
// @lc code=end
27+

125.验证回文串.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,49 @@
3636
* @param {string} s
3737
* @return {boolean}
3838
*/
39-
var isPalindrome = function (s) {
40-
let cop = s.toLocaleLowerCase().replace(/\W/g, '')
41-
let rev = cop.split('').reverse().join('')
42-
return cop === rev
39+
var isPalindrome = function (input) {
40+
var start = 0
41+
var end = input.length - 1
42+
while (start < end) {
43+
var s = input.charCodeAt(start)
44+
var e = input.charCodeAt(end)
45+
46+
if (!isLetter(s)) {
47+
start++
48+
continue
49+
}
50+
if (!isLetter(e)) {
51+
end--
52+
continue
53+
}
54+
55+
if (toLowerCase(s) !== toLowerCase(e)) {
56+
return false
57+
}
58+
start++
59+
end--
60+
}
61+
return true
62+
};
63+
64+
var isLetter = function(code) {
65+
if (((code >= 48) && (code <= 57)) // numbers
66+
|| ((code >= 65) && (code <= 90)) // uppercase
67+
|| ((code >= 97) && (code <= 122))) { // lowercase
68+
return true
69+
}
70+
else {
71+
return false
72+
}
73+
}
74+
75+
var toLowerCase = function(code) {
76+
if (code >= 65 && code <= 90) {
77+
return code + 32
78+
}
79+
else {
80+
return code
81+
}
4382
};
4483
// @lc code=end
4584

0 commit comments

Comments
 (0)