Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c36ab4

Browse files
committed
fixing conflicts
2 parents9cbc6c0 +6259876 commit3c36ab4

11 files changed

+238
-4
lines changed

‎Easy/104-maxDepth.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ var maxDepth = function(root) {
2222
}
2323
returnmax+1;
2424
}
25+
26+
// second try
27+
varmaxDepth=function(root){
28+
if(!root)return0;
29+
varlHeight=maxDepth(root.left)+1;
30+
varrHeight=maxDepth(root.right)+1;
31+
returnlHeight>rHeight ?lHeight :rHeight;
32+
};

‎Hard/115-distinctSubsequences.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Solution: Dynamic programming
3+
* match[i][j], the number of matched substrings when s with the length i and t with the length j.
4+
* if s[i-1] !== s[j-1], then the matched number equals to the matched number of s with length i - 1.
5+
* that is, match[i][j] = match[i-1][j].
6+
* if s[i-1] == s[j-1], the matched number is the mathched number when s with length i - 1 and t with length j,
7+
* plus the matched number when s with i - 1 length and t with j - 1 length.
8+
*
9+
*@param {string} s
10+
*@param {string} t
11+
*@return {number}
12+
*/
13+
varnumDistinct=function(s,t){
14+
if(!s)return0;
15+
16+
varmatch=[];
17+
for(vari=0;i<=s.length;i++){
18+
match[i]=[1];
19+
}
20+
21+
for(varj=1;j<=t.length;j++){
22+
match[0][j]=0;
23+
}
24+
25+
for(i=1;i<=s.length;i++){
26+
for(j=1;j<=t.length;j++){
27+
if(s[i-1]===t[j-1])match[i][j]=match[i-1][j-1]+match[i-1][j];
28+
elsematch[i][j]=match[i-1][j];
29+
}
30+
}
31+
32+
returnmatch[s.length][t.length];
33+
34+
};
35+
36+
// just use one dimension array to store matched numbers
37+
varnumDistinct=function(s,t){
38+
if(!s)return0;
39+
if(t.length>s.length)return0;
40+
41+
vartLength=t.length;
42+
varsLength=s.length;
43+
44+
vardp=[1];
45+
for(vari=1;i<=tLength;i++){
46+
dp.push(0);
47+
}
48+
49+
for(i=1;i<=sLength;i++){
50+
for(j=tLength;j>=1;j--){
51+
if(s[i-1]===t[j-1])dp[j]+=dp[j-1];
52+
}
53+
}
54+
55+
returndp[tLength];
56+
57+
};

‎Hard/132-palindromePartitioningII.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/**
1+
/**
22
*@param {string} s
33
*@return {number}
44
*/
5-
65
varminCut=function(s){
76
if(!s)return0;
87
varlength=s.length;
@@ -11,7 +10,7 @@ var minCut = function(s) {
1110
isPal[i]=[];
1211
for(varj=i;j<length;j++){
1312
if((j-i<=2||isPal[i+1][j-1])&&s[i]==s[j])
14-
isPal[i][j]=true;
13+
isPal[i][j]=true;
1514
}
1615
}
1716

‎Medium/106-constructBinaryTree.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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[]} inorder
10+
*@param {number[]} postorder
11+
*@return {TreeNode}
12+
*/
13+
varbuildTree=function(inorder,postorder){
14+
returnhelper(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
15+
};
16+
17+
varhelper=function(inorder,inStart,inEnd,postorder,postStart,postEnd){
18+
if(inEnd<inStart||postEnd<postStart)returnnull;
19+
varrootVal=postorder[postEnd];
20+
varroot=newTreeNode(rootVal);
21+
for(vari=0;i<inEnd;i++){
22+
if(inorder[i]===rootVal){
23+
break;
24+
}
25+
}
26+
root.left=helper(inorder,inStart,i-1,postorder,postStart,postStart+i-1-inStart);
27+
root.right=helper(inorder,i+1,inEnd,postorder,postEnd-inEnd+i,postEnd-1);
28+
returnroot;
29+
};

‎Medium/114-flattenTree.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,18 @@ var flatten = function(root) {
1313
while(oRoot.right)oRoot=oRoot.right;
1414
oRoot.right=oRight;
1515
};
16+
17+
// 2nd try
18+
varflatten=function(root){
19+
if(!root)return;
20+
// save right now
21+
vartmp=root.right;
22+
flatten(root.left);
23+
flatten(root.right);
24+
root.right=root.left;
25+
root.left=null;
26+
while(root.right){
27+
root=root.right;
28+
}
29+
root.right=tmp;
30+
};

‎Medium/120-triangle.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Dynamic programming
3+
*
4+
*@param {number[][]} triangle
5+
*@return {number}
6+
*/
7+
// straightforward, top-down, space O(N^2)
8+
varminimumTotal=function(triangle){
9+
vardp=[triangle[0]];
10+
for(vari=1;i<triangle.length;i++){
11+
// in JavaScript, remember to have a new array for each row.
12+
dp[i]=[];
13+
for(varj=0;j<triangle[i].length;j++){
14+
if(j===0)dp[i][j]=dp[i-1][j]+triangle[i][j];
15+
elseif(j===triangle[i].length-1)dp[i][j]=dp[i-1][j-1]+triangle[i][j];
16+
elsedp[i][j]=Math.min(dp[i-1][j-1],dp[i-1][j])+triangle[i][j];
17+
}
18+
}
19+
returnMath.min.apply(null,dp[triangle.length-1]);
20+
};
21+
22+
// bottom-up, space O(N)
23+
varminimumTotal=function(triangle){
24+
varlength=triangle.length;
25+
varsum=triangle[length-1];
26+
for(vari=length-2;i>=0;i--){
27+
for(varj=0;j<triangle[i].length;j++){
28+
sum[j]=Math.min(sum[j],sum[j+1])+triangle[i][j];
29+
}
30+
console.log(sum);
31+
}
32+
returnsum[0];
33+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*@param {string} str
3+
*@returns {string}
4+
*/
5+
varreverseWords=function(str){
6+
varwords=str.split(' ');
7+
// the O(1) space
8+
varj=0;
9+
for(vari=0;i<words.length;i++){
10+
if(words[i]!==''){
11+
words[j]=words[i];
12+
j++;
13+
}
14+
}
15+
returnwords.slice(0,j).reverse().join(' ');
16+
};

‎Medium/152-MaxProductSubarray.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Solution: Two dp arrays: maxDP tracks the Maximum product at index i,
3+
* minDP tracks the Minimum product at index i, either of them can be negative Numbers
4+
* so if nums[i + 1] is negative, then maxDP[i+1] can be a product of minDP[i] * nums[i+1]
5+
*@see the for loop body
6+
*
7+
*@param {number[]} nums
8+
*@return {number}
9+
*/
10+
varmaxProduct=function(nums){
11+
if(nums.length===0)return0;
12+
varresult=nums[0];
13+
varmaxDP=[nums[0]];
14+
varminDP=[nums[0]];
15+
16+
for(vari=1;i<nums.length;i++){
17+
maxDP[i]=Math.max(nums[i],maxDP[i-1]*nums[i],minDP[i-1]*nums[i]);
18+
minDP[i]=Math.min(nums[i],minDP[i-1]*nums[i],maxDP[i-1]*nums[i]);
19+
result=Math.max(maxDP[i],result);
20+
}
21+
22+
returnresult;
23+
};

‎Medium/5-longestPalindromicSubstring.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,29 @@ var getPalindromeSubstring = function(s, i, j) {
3434

3535
returns.substring(i+1,j);
3636
};
37+
38+
39+
// dp method, should work. memory exceed limit
40+
// bottom-up.
41+
varlongestPalindrome=function(s){
42+
if(!s)returns;
43+
varlongest=s.substring(0,1);
44+
varlength=s.length;
45+
vardp=[];
46+
varmaxLength=1;
47+
for(vari=length-1;i>=0;i--){
48+
dp[i]=[];
49+
for(varj=i;j<length;j++){
50+
// j - i <= 2 is important, in case i + 1 is out of array boundary.
51+
if(s[i]===s[j]&&(j-i<=2||dp[i+1][j-1])){
52+
dp[i][j]=true;
53+
if(j-i+1>maxLength){
54+
maxLength=j-i+1;
55+
longest=s.substring(i,j+1);
56+
}
57+
}
58+
}
59+
}
60+
61+
returnlongest;
62+
};

‎Medium/91-decodeways.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* solution: dp[i] = dp[i - 1] if s[i - 1] is valid (!= 0)
3+
* or dp[i] = dp[i-2] if s[i-1] is not valid, however s.substring(i-2, i) is valid ( >=10 && <=26)
4+
* or dp[i] = dp[i-1] + dp[i-2] is both s.substring(i-2, i), s.substring(i-1, i) are valid
5+
*
6+
*@param {string} s
7+
*@return {number}
8+
*/
9+
varnumDecodings=function(s){
10+
if(s.length===0)return0;
11+
vardp=[1];
12+
s[0]==='0' ?dp[1]=0 :dp[1]=1;
13+
14+
for(vari=2;i<=s.length;i++){
15+
varprevTwo=parseInt(s.substring(i-2,i));
16+
dp[i]=0;
17+
if(s[i-1]!=='0')dp[i]=dp[i-1];
18+
if(prevTwo>=10&&prevTwo<=26)dp[i]+=dp[i-2];
19+
}
20+
21+
returndp[s.length];
22+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp