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

Commit2ccc7ee

Browse files
committed
add four mediums
1 parentd0b1959 commit2ccc7ee

File tree

6 files changed

+141
-8
lines changed

6 files changed

+141
-8
lines changed

‎Medium/24-swapNodesPairs.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@return {ListNode}
11+
*/
12+
varswapPairs=function(head){
13+
if(!head||!head.next)returnhead;
14+
varp=head;
15+
varpNext=head.next;
16+
varnewHead=pNext;
17+
while(pNext){
18+
p.next=pNext.next;
19+
pNext.next=p;
20+
varnextPairHead=p;
21+
p=p.next;
22+
if(p){
23+
pNext=p.next;
24+
if(pNext)nextPairHead.next=pNext;
25+
elsenextPairHead.next=p;
26+
}else{
27+
pNext=null;
28+
}
29+
}
30+
31+
returnnewHead;
32+
};

‎Medium/46-permutations.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* permutation of A[1..n] equals to
3+
* A[1] + permutation of (A[1..n] - A[1])
4+
* A[2] + permutation of (A[1..n] - A[2])
5+
* ...
6+
* A[n] + permutation of (A[1..n] - A[n])
7+
* Everytime, swap the beginning element with current element, and do the permutation
8+
* Once the passed nums array is changed, we should have a deep copy of the array and push
9+
* it to the final results array.
10+
*
11+
*@param {number[]} nums
12+
*@return {number[][]}
13+
*/
14+
varpermute=function(nums){
15+
if(nums.length===0)returnnums;
16+
varresults=[];
17+
permuteHelper(0,nums,results);
18+
returnresults;
19+
};
20+
21+
varpermuteHelper=function(start,nums,results){
22+
if(start>=nums.length){
23+
results.push(deepCopyArray(nums));
24+
return;
25+
}
26+
27+
for(vari=start;i<nums.length;i++){
28+
swap(nums,start,i);
29+
permuteHelper(start+1,nums,results);
30+
swap(nums,start,i);
31+
}
32+
};
33+
34+
varswap=function(nums,i,j){
35+
if(i===j)return;
36+
vartmp=nums[i];
37+
nums[i]=nums[j];
38+
nums[j]=tmp;
39+
};
40+
41+
vardeepCopyArray=function(nums){
42+
varnewNums=[];
43+
for(vari=0;i<nums.length;i++){
44+
newNums[i]=nums[i];
45+
}
46+
returnnewNums;
47+
};

‎Medium/62-uniquePaths.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Let t be total movements from (1, 1) to (m, n), t = m - 1 + n - 1;
3+
* Let k = m - 1 (the right movements)
4+
* then the total path is all combinations (m - 1) movements from (m + n - 2)
5+
* And C(t, k) = t! / (t-k)! * k! = (t * (t - 1) ... (t - k + 1)) / (1 * ... * k)
6+
*
7+
*@param {number} m
8+
*@param {number} n
9+
*@return {number}
10+
*/
11+
varuniquePaths=function(m,n){
12+
vart=m+n-2;
13+
vark=m-1;
14+
varres=1;
15+
16+
for(vari=1;i<=k;i++){
17+
res*=(t-i+1)/i
18+
}
19+
20+
returnres;
21+
};

‎Medium/77-combinations.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Same idea as permutation
3+
*
4+
*@param {number} n
5+
*@param {number} k
6+
*@return {number[][]}
7+
*/
8+
varcombine=function(n,k){
9+
varresult=[];
10+
varresults=[];
11+
combineHelper(n,k,1,result,results);
12+
returnresults;
13+
};
14+
15+
varcombineHelper=function(n,k,start,result,results){
16+
if(k===0){
17+
results.push(deepCopyArray(result));
18+
return;
19+
}
20+
21+
for(vari=start;i<=n;i++){
22+
result.push(i);
23+
// increase i here
24+
combineHelper(n,k-1,i+1,result,results);
25+
result.pop();
26+
}
27+
};
28+
29+
vardeepCopyArray=function(nums){
30+
varnewNums=[];
31+
for(vari=0;i<nums.length;i++){
32+
newNums[i]=nums[i];
33+
}
34+
returnnewNums;
35+
};

‎Medium/list.sh‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
####LeetCode Solutions in JavaScript
22
* Tried to use optimal solutions as much as I can. Most solutions are from the LeetCode community.
3-
* Written in[Airbnb JavaScript](https://github.com/airbnb/javascript) style.
3+
* Written in[Airbnb JavaScript](https://github.com/airbnb/javascript) style.
44
* A work in progress.
55
* If you find something wrong or the complexity is not good, you are very welcome to contribute your better solutions.
66

@@ -11,6 +11,7 @@
1111
*[14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) -[Solution](./Easy/14-longestCommonPrefix.js)
1212
*[19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) -[Solution](./Easy/19-removeNthNodeFromEndofList.js)
1313
*[21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) -[Solution](./Easy/21-mergeSortedLists.js)
14+
*[24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -[Solution](./Easy/24-swapNodesPairs.js)
1415
*[28. Implement strStr()](https://leetcode.com/problems/implement-strstr/) -[Solution](./Easy/28-implementstrStr.js)
1516
*[38. Count and Say](https://leetcode.com/problems/count-and-say/) -[Solution](./Easy/38-countandSay.js)
1617
*[66. Plus One](https://leetcode.com/problems/plus-one/) -[Solution](./Easy/66-plusOne.js)
@@ -63,8 +64,12 @@
6364

6465
#####Medium
6566
*[3. Longest Substring Without Repeating Characters](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) -[Solution](./Medium/3-lengthOfLongestSubstring.js)
67+
*[22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) -[Solution](./Medium/22-generateParentheses.js)
6668
*[35. Search Insert Position Characters](https://oj.leetcode.com/problems/search-insert-position/) -[Solution](./Medium/35-searchInsert.js)
69+
*[46. Permutations](https://leetcode.com/problems/permutations/) -[Solution](./Medium/46-permutations.js)
6770
*[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) -[Solution](./Medium/53-maximumSubarray.js)
71+
*[62.Unique Paths](https://leetcode.com/problems/unique-paths/) -[Solution](./Medium/62-uniquePaths.js)
72+
*[77.Combinations](https://leetcode.com/problems/combinations/) -[Solution](./Medium/77-combinations.js)
6873
*[94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) -[Solution](./Medium/94-binaryTreeInorder.js)
6974
*[96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) -[Solution](./Medium/96-uniqueBinarySearchTrees.js)
7075
*[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) -[Solution](./Medium/108-convertSortedArraytoBST.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp