Skip to content

Commit 2e9a88c

Browse files
committed
revisit some questions
1 parent e06031b commit 2e9a88c

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

Easy/160-IntersectionofTwoLinkedLists.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,67 @@ var getLength = function(listHead) {
4444
}
4545
return length;
4646
};
47+
48+
// 2nd try without knowing the length
49+
var getIntersectionNode = function(headA, headB) {
50+
var intersection = null;
51+
var pa = headA;
52+
var pb = headB;
53+
54+
if (!pa || !pb) {
55+
return intersection;
56+
}
57+
58+
while (pa || pb) {
59+
if (pa && pb && pa.val === pb.val) {
60+
intersection = pa;
61+
}
62+
63+
// this can be replace by pa === pb
64+
while (pa && pb && pa.val === pb.val) {
65+
pa = pa.next;
66+
pb = pb.next;
67+
}
68+
69+
if (pa === null && pb === null) {
70+
break;
71+
} else if (pa === null) {
72+
pa = headB;
73+
} else if (pb === null) {
74+
pb = headA;
75+
} else {
76+
pa = pa.next;
77+
pb = pb.next;
78+
}
79+
}
80+
81+
return intersection;
82+
};
83+
84+
// more concise version, compared to version 2
85+
var getIntersectionNode = function(headA, headB) {
86+
var pa = headA;
87+
var pb = headB;
88+
89+
if (!pa || !pb) {
90+
return null;
91+
}
92+
93+
while (pa && pb && pa !== pb) {
94+
pa = pa.next;
95+
pb = pb.next;
96+
if (pa === pb) {
97+
return pa ;
98+
}
99+
100+
if (!pa) {
101+
pa = headB;
102+
}
103+
104+
if (!pb) {
105+
pb = headA;
106+
}
107+
}
108+
109+
return pa;
110+
};

Medium/152-MaxProductSubarray.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ var maxProduct = function(nums) {
2121

2222
return result;
2323
};
24+
25+
// 2nd try
26+
var maxProduct = function(nums) {
27+
var min = [nums[0]];
28+
var max = [nums[0]];
29+
var maxProduct = nums[0];
30+
31+
for (var i = 1; i < nums.length; i++) {
32+
min.push(Math.min(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]));
33+
max.push(Math.max(nums[i], min[i - 1] * nums[i], max[i - 1] * nums[i]));
34+
maxProduct = Math.max(maxProduct, max[i]);
35+
}
36+
37+
return maxProduct;
38+
};

Medium/39-combinationSum.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
* @return {number[][]}
77
*/
88
var combinationSum = function(candidates, target) {
9+
var result = [];
10+
var results = [];
11+
912
candidates.sort(function(a, b) {
1013
return a - b;
1114
});
12-
var result = [];
13-
var results = [];
1415
combinationSumHelper(candidates, target, results, result, 0);
1516
return results;
1617
};

Medium/40-combinationSumII.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
* @return {number[][]}
55
*/
66
var combinationSum2 = function(candidates, target) {
7+
var result = [];
8+
var results = [];
9+
710
candidates.sort(function(a, b) {
811
return a - b;
912
});
10-
var result = [];
11-
var results = [];
1213
combinationSum2Helper(candidates, result, results, target, 0);
1314
return results;
1415
};
1516

1617
var combinationSum2Helper = function(candidates, result, results, target, start) {
1718
if (target === 0) {
18-
results.push(deepCopy(result.slice()));
19+
results.push(result.slice());
1920
return;
2021
}
2122

Medium/50-powerxn.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66
var myPow = function(x, n) {
77
if (n === 0) return 1;
88
if (n < 0) {
9-
n = -1 * n;
9+
n *= -1;
1010
x = 1 / x;
1111
}
1212
return n % 2 === 0 ? myPow(x*x, n / 2) : x * myPow(x * x, Math.floor(n / 2));
1313
};
14+
15+
// runtime error?
16+
var myPow = function(x, n) {
17+
if (n === 0) {
18+
return 1;
19+
}
20+
21+
if (n === 1) {
22+
return x;
23+
}
24+
25+
return n > 0 ? myPow(x, n - 1) * x : x / myPow(x, -n - 1) * x;
26+
};

0 commit comments

Comments
 (0)