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

Commitdfbaf07

Browse files
committed
2 parents0816077 +b44b956 commitdfbaf07

9 files changed

+277
-1
lines changed

‎Easy/344-reverseString.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
*@param {string} s
3+
*@return {string}
4+
*/
5+
varreverseString=function(s){
6+
returns.split('').reverse().join('');
7+
};

‎Easy/349-intersectionTwoArrays.js‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
*@param {number[]} nums1
3+
*@param {number[]} nums2
4+
*@return {number[]}
5+
*/
6+
// use hashset idea, O(N)
7+
varintersection=function(nums1,nums2){
8+
// Note ES5 doesn't have set
9+
varset1=newSet();
10+
varinterSet=newSet();
11+
for(vari=0;i<nums1.length;i++){
12+
set1.add(nums1[i]);
13+
}
14+
15+
for(varj=0;j<nums2.length;j++){
16+
if(set1.has(nums2[j])){
17+
interSet.add(nums2[j]);
18+
}
19+
}
20+
21+
varresult=[];
22+
interSet.forEach(function(item){
23+
result.push(item);
24+
});
25+
26+
returnresult;
27+
};
28+
29+
// solution 2: sort first. O(nlogn)
30+
varintersection=function(nums1,nums2){
31+
// Note ES5 doesn't have set
32+
varinterSet=newSet();
33+
nums1.sort(comparator);
34+
nums2.sort(comparator);
35+
vari=0;
36+
varj=0;
37+
38+
while(i<nums1.length){
39+
if(j===nums2.length)break;
40+
if(nums1[i]<nums2[j]){
41+
i++;
42+
}elseif(nums1[i]>nums2[j]){
43+
j++;
44+
}else{
45+
interSet.add(nums1[i]);
46+
i++;
47+
j++;
48+
}
49+
}
50+
51+
varresult=[];
52+
interSet.forEach(function(item){
53+
result.push(item);
54+
});
55+
56+
returnresult;
57+
};
58+
59+
varcomparator=function(a,b){
60+
returna-b;
61+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
*@param {number[]} nums1
3+
*@param {number[]} nums2
4+
*@return {number[]}
5+
*/
6+
varintersect=function(nums1,nums2){
7+
varhashMap={};
8+
for(vari=0;i<nums1.length;i++){
9+
if(hashMap.hasOwnProperty(nums1[i])){
10+
hashMap[nums1[i]]++;
11+
}else{
12+
hashMap[nums1[i]]=1;
13+
}
14+
}
15+
varhashMap2={};
16+
for(i=0;i<nums2.length;i++){
17+
if(hashMap2.hasOwnProperty(nums2[i])){
18+
hashMap2[nums2[i]]++;
19+
}else{
20+
hashMap2[nums2[i]]=1;
21+
}
22+
}
23+
24+
varresult=[];
25+
for(varkeyinhashMap){
26+
if(!hashMap2.hasOwnProperty(key))continue;
27+
varnumAppears=Math.min(hashMap[key],hashMap2[key]);
28+
for(i=0;i<numAppears;i++){
29+
result.push(parseInt(key));
30+
}
31+
}
32+
33+
returnresult;
34+
};
35+
36+
// A better and more concise solution. One hashmap needed.
37+
varintersect=function(nums1,nums2){
38+
varhashMap={};
39+
for(vari=0;i<nums1.length;i++){
40+
if(hashMap.hasOwnProperty(nums1[i])){
41+
hashMap[nums1[i]]++;
42+
}else{
43+
hashMap[nums1[i]]=1;
44+
}
45+
}
46+
47+
varresult=[];
48+
for(i=0;i<nums2.length;i++){
49+
if(hashMap.hasOwnProperty(nums2[i])&&hashMap[nums2[i]]>0){
50+
result.push(nums2[i]);
51+
hashMap[nums2[i]]--;
52+
}
53+
}
54+
55+
returnresult;
56+
};

‎Medium/129-sumRootToLeafNumbers.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,19 @@ var helper = function(root, sum) {
4747
returnhelper(root.left,10*sum+root.val)
4848
+helper(root.right,10*sum+root.val);
4949
};
50+
51+
// a third DFS method. Top-down
52+
varsumNumbers=function(root){
53+
if(!root)return0;
54+
returnhelper(root,0,0);
55+
};
56+
57+
functionhelper(root,currNum,sum){
58+
if(!root)returnsum;
59+
currNum=root.val+10*currNum;
60+
if(!root.left&&!root.right){
61+
sum+=currNum;
62+
returnsum;
63+
}
64+
returnhelper(root.left,currNum,sum)+helper(root.right,currNum,sum)
65+
}

‎Medium/147-insertionSortList.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
varinsertionSortList=function(head){
13+
if(!head)returnhead;
14+
vardummyHead=newListNode(null);
15+
dummyHead.next=head;
16+
varfirst=dummyHead;
17+
varsecond=head;
18+
19+
while(second.next){
20+
if(second.next.val<second.val){
21+
varsmallNode=second.next;
22+
second.next=second.next.next;
23+
while(first.next&&first.next.val<smallNode.val){
24+
first=first.next;
25+
}
26+
smallNode.next=first.next;
27+
first.next=smallNode;
28+
first=dummyHead;
29+
}else{
30+
second=second.next;
31+
if(!second)break;
32+
}
33+
34+
}
35+
36+
returndummyHead.next;
37+
};

‎Medium/148-sortList.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* sort, then merge. In fact, this method use O(logN) space for recursive function calls.
10+
* needs to be improved.
11+
*
12+
*@param {ListNode} head
13+
*@return {ListNode}
14+
*/
15+
varsortList=function(head){
16+
if(!head||!head.next)returnhead;
17+
varfast=head;
18+
varslow=head;
19+
20+
while(fast.next&&fast.next.next){
21+
slow=slow.next;
22+
fast=fast.next.next;
23+
}
24+
fast=slow.next;
25+
slow.next=null;
26+
varsecondHalf=sortList(fast);
27+
varfirstHalf=sortList(head);
28+
returnmerge(firstHalf,secondHalf);
29+
};
30+
31+
varmerge=function(l1,l2){
32+
varl3=newListNode();
33+
varl3Head=l3;
34+
while(l1&&l2){
35+
if(l1.val<=l2.val){
36+
l3.next=l1;
37+
l1=l1.next;
38+
}else{
39+
l3.next=l2;
40+
l2=l2.next;
41+
}
42+
l3=l3.next;
43+
}
44+
if(!l1)l3.next=l2;
45+
if(!l2)l3.next=l1;
46+
47+
returnl3Head.next;
48+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*@param {string[]} tokens
3+
*@return {number}
4+
*/
5+
// seems like it is not a good solution
6+
varevalRPN=function(tokens){
7+
varstack=[];
8+
for(vari=0;i<tokens.length;i++){
9+
vartoken=tokens[i];
10+
if(!isNaN(token)){
11+
stack.push(token);
12+
}else{
13+
varnum1=stack.pop();
14+
varnum2=stack.pop();
15+
varresult=getResult(num2,num1,token);
16+
stack.push(result);
17+
}
18+
}
19+
20+
returnMath.floor(parseInt(stack[stack.length-1]));
21+
};
22+
23+
vargetResult=function(num1,num2,operator){
24+
vara=parseInt(num1);
25+
varb=parseInt(num2);
26+
if(operator==='+')returna+b;
27+
if(operator==='-')returna-b;
28+
if(operator==='/')returna/b;
29+
if(operator==='*')returna*b;
30+
};

‎Medium/338-countingBits.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Solution: f(i) = f(i/2) + i % 2;
3+
* the bit nums = the bit nums of half of the number, and plus 1 if the last digit of the bit number is 1.
4+
*
5+
*@param {number} num
6+
*@return {number[]}
7+
*/
8+
varcountBits=function(num){
9+
varresult=[0];
10+
for(vari=1;i<=num;i++){
11+
result[i]=result[i>>1]+(i&1);
12+
}
13+
14+
returnresult;
15+
};

‎README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@
6868
*[326. Power of Three](https://leetcode.com/problems/power-of-three/) -[Solution](./Easy/326-powerOfThree.js)
6969
*[328. Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) -[Solution](./Easy/328-oddevenLinkedList.js)
7070
*[342. Power of Four](https://leetcode.com/problems/power-of-four/) -[Solution](./Easy/342-powerOfFour.js)
71+
*[344. Reverse String](https://leetcode.com/problems/reverse-string/) -[Solution](./Easy/344-reverseString.js)
72+
*[349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) -[Solution](./Easy/349-intersectionTwoArrays.js)
73+
*[350. Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) -[Solution](./Easy/349-intersectionTwoArraysII.js)
7174
*[412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/?tab=Solutions) -[Solution](./Easy/412-fizzBuzz.js)
7275

73-
7476
#####Medium
7577
*[1. Two Sum](https://leetcode.com/problems/two-sum/) -[Solution](./Medium/1-twoSum.js)
7678
*[2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) -[Solution](./Medium/2-addTwoNumbers.js)
@@ -145,6 +147,9 @@
145147
*[142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) -[Solution](./Medium/142-linkedListCycleII.js)
146148
*[143. Reorder List](https://leetcode.com/problems/reorder-list/) -[Solution](./Medium/143-reorderList.js)
147149
*[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -[Solution](./Medium/144-binaryTreePreorder.js)
150+
*[147. Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) -[Solution](./Medium/147-insertionSortList.js)
151+
*[148. Sort List](https://leetcode.com/problems/sort-list/) -[Solution](./Medium/148-sortList.js)
152+
*[150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) -[Solution](./Medium/150-reversePolishNotation.js)
148153
*[151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) -[Solution](./Medium/151-reverseWordsInAString.js)
149154
*[152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) -[Solution](./Medium/152-MaxProductSubarray.js)
150155
*[153.Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) -[Solution](./Medium/153-findMinimumInRotatedSortedArray.js)
@@ -171,6 +176,7 @@
171176
*[319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) -[Solution](./Medium/319-bulbSwitcher.js)
172177
*[331. Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) -[Solution](./Medium/331-verifyBinaryTree.js)
173178
*[337. House Robber III](https://leetcode.com/problems/house-robber-iii/) -[Solution](./Medium/337-houseRobberIII.js)
179+
*[338. Counting Bits](https://leetcode.com/problems/counting-bits/) -[Solution](./Medium/338-countingBits.js)
174180
*[347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) -[Solution](./Medium/247-topKFrequentElements.js)
175181

176182
#####Hard

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp