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

Commit82282bd

Browse files
committed
update solutions, add 60 permutation sequence
1 parent4923264 commit82282bd

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

‎Easy/102-binaryTreeLevelOrder.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var levelOrder = function(root) {
7272

7373
varhelper=function(results,node,level){
7474
if(!node)returnresults;
75-
if(level>=results.length){
75+
if(level===results.length){
7676
results[level]=[];
7777
}
7878
results[level].push(node.val);

‎Medium/139-wordBreak.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*@return {boolean}
77
*/
88

9-
// recursion, not accepted, time exceeds limits.
9+
// recursion, not accepted, time exceeds limits. O(N^2)
1010
varwordBreak=function(s,wordDict){
1111
returnhelper(s,wordDict,0);
1212
};
@@ -22,3 +22,19 @@ var helper = function(s, wordDict, start) {
2222
}
2323
returnfalse;
2424
};
25+
26+
// Dynamic, accepted
27+
varwordBreak=function(s,wordDict){
28+
varcanBreak=[true];
29+
for(vari=0;i<s.length;i++){
30+
if(!canBreak[i])continue;
31+
for(varwordofwordDict){
32+
varwLength=word.length;
33+
if(canBreak[i+wLength])continue;
34+
if(s.substring(i,i+wLength)===word){
35+
canBreak[i+wLength]=true;
36+
}
37+
}
38+
}
39+
returncanBreak[s.length] ?true :false;
40+
};

‎Medium/144-binaryTreePreorder.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ var preorderTraversal = function(root) {
2424
}
2525
returnorder;
2626
};
27+
28+
// this is a more straightforward method, but slower than first one.
29+
varpreorderTraversal=function(root){
30+
if(!root)return[];
31+
varresult=[];
32+
varstack=[root];
33+
34+
while(stack.length>0){
35+
varnode=stack.pop();
36+
result.push(node.val);
37+
if(node.right){
38+
stack.push(node.right);
39+
}
40+
if(node.left){
41+
stack.push(node.left);
42+
}
43+
}
44+
45+
returnresult;
46+
};

‎Medium/60-permutationSequence.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*@see inline comments
3+
*
4+
*@param {number} n
5+
*@param {number} k
6+
*@return {string}
7+
*/
8+
vargetPermutation=function(n,k){
9+
varnums=[];
10+
for(vari=0;i<n;i++){
11+
nums.push(i+1);
12+
}
13+
k--;
14+
varmod=1;
15+
for(i=1;i<n;i++){
16+
mod*=i;
17+
}
18+
varresult='';
19+
for(i=0;i<n;i++){
20+
// find the index of current number's first digit
21+
varindex=Math.floor(k/mod);
22+
k=k%mod;
23+
result+=nums[index];
24+
// remove this used number from nums.
25+
nums.splice(index,1);
26+
mod=Math.floor(mod/(n-i-1));
27+
}
28+
29+
returnresult;
30+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
*[54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) -[Solution](./Medium/54-spiralMatrix.js)
9595
*[55. Jump Game](https://leetcode.com/problems/jump-game/) -[Solution](./Medium/55-jumpGame.js)
9696
*[59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) -[Solution](./Medium/59-spiralMatrixII.js)
97+
*[60. Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) -[Solution](./Medium/60-permutationSequence.js)
9798
*[61. Rotate List](https://leetcode.com/problems/rotate-list/) -[Solution](./Medium/61-rotateList.js)
9899
*[62.Unique Paths](https://leetcode.com/problems/unique-paths/) -[Solution](./Medium/62-uniquePaths.js)
99100
*[63.Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) -[Solution](./Medium/63-uniquePathsII.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp