Skip to content

Commit 4ac4b4a

Browse files
author
zeyap
committed
init
1 parent 2d3a705 commit 4ac4b4a

Some content is hidden

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

115 files changed

+3080
-0
lines changed

100.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var isSameTree = function(p, q) {
2+
if(p === null && q=== null){return true;}
3+
if(p===null||q===null){return false;}
4+
if(p.val === q.val){
5+
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
6+
}else{
7+
return false;
8+
}
9+
};

102.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var levelOrder = function(root) {
2+
let res=[];
3+
if(root===null)return res;
4+
let stk=[{node:root, d:0}];
5+
while(stk.length>0){
6+
let curr = stk.shift();
7+
res[curr.d] = res[curr.d]||[];
8+
res[curr.d].push(curr.node.val);
9+
if(curr.node.left!==null){
10+
stk.push({node:curr.node.left,d:curr.d+1});
11+
}
12+
if(curr.node.right!==null){
13+
stk.push({node:curr.node.right,d:curr.d+1});
14+
}
15+
}
16+
return res;
17+
};

103.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var zigzagLevelOrder = function(root) {
2+
if(root===null)return [];
3+
let stk = [root];
4+
let toRight = false;
5+
let ans = [];
6+
while(stk.length>0){
7+
let levelNum = stk.length;
8+
let level = [];
9+
for(let i=0;i<levelNum;i++){
10+
let curr = stk.shift();
11+
level.push(curr.val);
12+
if(curr.left)stk.push(curr.left);
13+
if(curr.right)stk.push(curr.right);
14+
}
15+
if(toRight) level.reverse();
16+
ans.push(level);
17+
toRight = !toRight;
18+
}
19+
return ans;
20+
};

104.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Depth first search
2+
3+
var maxDepth = function(root) {
4+
5+
var dfs = function(root){
6+
if(root===null)return 0;
7+
var leftDepth = 1+dfs(root.left);
8+
var rightDepth = 1+dfs(root.right);
9+
return leftDepth>rightDepth?leftDepth:rightDepth;
10+
}
11+
return dfs(root);
12+
};
13+
14+
// Breath first search
15+
16+
var maxDepth = function(root) {
17+
if(root===null)return 0;
18+
var q=[];
19+
q.push(root);
20+
var depth=0;
21+
while(q.length!==0){
22+
depth++;
23+
var levelNum = q.length;
24+
for(var i=0;i<levelNum;i++){
25+
var currNode = q.shift();
26+
if(currNode.left!==null)q.push(currNode.left);
27+
if(currNode.right!==null)q.push(currNode.right);
28+
}
29+
}
30+
return depth;
31+
};
32+

107.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//BFS
2+
3+
var levelOrderBottom = function(root) {
4+
if(root===null)return [];
5+
let stk=[root];
6+
let res=[];
7+
let curr = root;
8+
while(stk.length>0){
9+
res.unshift(stk.map((item)=>item.val));
10+
let levelNum = stk.length;
11+
for(let i=0;i<levelNum;i++){
12+
curr = stk.shift();
13+
if(curr.left!==null)stk.push(curr.left);
14+
if(curr.right!==null)stk.push(curr.right);
15+
}
16+
}
17+
return res;
18+
};
19+
20+
//DFS
21+
22+
var levelOrderBottom = function(root) {
23+
if(root===null)return [];
24+
let res=[];
25+
DFS(root,0,res);
26+
return res.reverse();
27+
};
28+
29+
var DFS = function(root,level,res){
30+
if(root===null)return;
31+
res[level]=res[level]||[];
32+
res[level].push(root.val);
33+
DFS(root.left,level+1,res);
34+
DFS(root.right,level+1,res);
35+
return;
36+
}

108.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var sortedArrayToBST = function(nums) {
2+
if(nums.length===0)return null;
3+
return builder(nums,0, nums.length-1);
4+
};
5+
6+
var builder = function(nums,lo,hi){
7+
if(lo>hi)return null;
8+
let mid = parseInt((lo+hi)/2);
9+
let root=new TreeNode(nums[mid]);
10+
root.val = nums[mid];
11+
root.left=builder(nums,lo,mid-1);
12+
root.right = builder(nums,mid+1,hi);
13+
return root;
14+
}

11.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
var maxArea = function(height) {
3+
var maxar=0;
4+
var head=0,tail=height.length-1;
5+
var h,area;
6+
while(head<tail){
7+
area = Math.min(height[head],height[tail])*(tail-head);
8+
if(area>maxar){
9+
maxar = area;
10+
}
11+
h = Math.min(height[head],height[tail]);
12+
while(height[head]<=h)head++;
13+
while(height[tail]<=h)tail--;
14+
}
15+
16+
return maxar;
17+
};
18+
19+
//========fastest======
20+
21+
var maxArea = function(height) {
22+
var maxar=0;
23+
var head=0,tail=height.length-1;
24+
var h,area;
25+
while(head<tail){
26+
maxar=Math.max(maxar,Math.min(height[head],height[tail])*(tail-head));
27+
if(height[head]<height[tail]){head++;}
28+
else{tail--;}
29+
}
30+
31+
return maxar;
32+
};

118.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var generate = function(numRows) {
2+
let res = [];
3+
for(let i=0;i<numRows;i++){
4+
res[i]=[];
5+
if(i===0){res[i]=[1];continue;}
6+
for(let j=0;j<i+1;j++){
7+
let x = res[i-1][j-1]||0;
8+
let y = res[i-1][j]||0;
9+
res[i][j] = x+y;
10+
}
11+
}
12+
return res;
13+
};

119.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var getRow = function(rowIndex) {
2+
let row = [];
3+
for(let i=0;i<=rowIndex;i++){
4+
let curr = [];
5+
let rear= Math.floor(i/2);
6+
let prevRear = i%2===0?rear-1:rear;
7+
for(let j=0;j<=rear;j++){
8+
if(j===0)curr[j]=1;
9+
else if(j<=prevRear){
10+
curr[j] = row[j-1]+row[j];
11+
}else{
12+
curr[j] = row[prevRear]*2;
13+
}
14+
}
15+
row = curr;
16+
curr = [];
17+
}
18+
let rev=[];
19+
for(let i=row.length-1;i>=0;i--){
20+
rev[row.length-1-i] = row[i];
21+
}
22+
return rowIndex%2===0?row.concat(rev.slice(1)):row.concat(rev);
23+
};

121.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// One pass
2+
var maxProfit = function(prices) {
3+
let lowestPrice=Number.MAX_VALUE,maxProfit=0;
4+
for(let i=0;i<prices.length;i++){
5+
if(prices[i]<lowestPrice){
6+
lowestPrice = prices[i];
7+
}else{
8+
let profit = prices[i]-lowestPrice;
9+
if(profit>maxProfit){
10+
maxProfit = profit;
11+
}
12+
}
13+
}
14+
return maxProfit;
15+
};
16+
17+
// Kadane's algorithm
18+
var maxProfit = function(prices) {
19+
let diffs=prices.map((price,id)=>{
20+
if(id===0)return 0;
21+
else return price-prices[id-1];
22+
})
23+
let maxProfit=0;
24+
let maxSoFar=0;
25+
for(let i=0;i<diffs.length;i++){
26+
maxProfit=diffs[i]>maxProfit+diffs[i]?diffs[i]:maxProfit+diffs[i];
27+
maxSoFar=(maxProfit>maxSoFar)?maxProfit:maxSoFar;
28+
}
29+
return maxSoFar;
30+
};
31+

0 commit comments

Comments
 (0)