Skip to content

Commit 1ca670c

Browse files
Run Prettier Batch 1
1 parent 1e9c31c commit 1ca670c

File tree

6 files changed

+101
-106
lines changed

6 files changed

+101
-106
lines changed

LeetcodeProblems/Algorithms/3Sum.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,33 @@ A solution set is:
2323
* @param {number[]} nums
2424
* @return {number[][]}
2525
*/
26-
var threeSum = function(nums) {
26+
var threeSum = function (nums) {
2727
var ret = [];
28-
nums.sort(function(a, b) { return a - b });
29-
for(var i = 0; i < nums.length; i++) {
30-
if(i === 0 || i > 0 && nums[i] !== nums[i - 1]) {
31-
var left = i + 1;
32-
var right = nums.length - 1;
33-
while(left < right) {
34-
const sum = nums[left] + nums[right] + nums[i];
35-
if(left > i + 1 && nums[left] === nums[left - 1] || sum < 0) {
36-
left++
37-
} else if(right < nums.length - 1 && nums[right] === nums[right + 1] || sum > 0) {
38-
right--;
39-
} else if(sum === 0) {
40-
ret.push([nums[left], nums[right], nums[i]]);
41-
left++;
42-
right--;
43-
}
44-
}
28+
nums.sort(function (a, b) {
29+
return a - b;
30+
});
31+
for (var i = 0; i < nums.length; i++) {
32+
if (i === 0 || (i > 0 && nums[i] !== nums[i - 1])) {
33+
var left = i + 1;
34+
var right = nums.length - 1;
35+
while (left < right) {
36+
const sum = nums[left] + nums[right] + nums[i];
37+
if ((left > i + 1 && nums[left] === nums[left - 1]) || sum < 0) {
38+
left++;
39+
} else if (
40+
(right < nums.length - 1 && nums[right] === nums[right + 1]) ||
41+
sum > 0
42+
) {
43+
right--;
44+
} else if (sum === 0) {
45+
ret.push([nums[left], nums[right], nums[i]]);
46+
left++;
47+
right--;
48+
}
4549
}
50+
}
4651
}
47-
52+
4853
return ret;
4954
};
5055

LeetcodeProblems/Algorithms/Add_Two_Numbers.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,41 @@ Explanation: 342 + 465 = 807.
2121
* this.next = null;
2222
* }
2323
*/
24-
var ListNode = require('../../UtilsClasses/ListNode').ListNode;
24+
var ListNode = require("../../UtilsClasses/ListNode").ListNode;
2525

2626
/**
2727
* @param {ListNode} l1
2828
* @param {ListNode} l2
2929
* @return {ListNode}
3030
*/
3131

32-
var addTwoNumbers = function(l1, l2) {
33-
if(l1 === null)
34-
return (l2 === null) ? new ListNode(0) : l2;
35-
else if(l2 === null)
36-
return l1;
37-
32+
var addTwoNumbers = function (l1, l2) {
33+
if (l1 === null) return l2 === null ? new ListNode(0) : l2;
34+
else if (l2 === null) return l1;
35+
3836
var elem = l1.val + l2.val;
3937
var number = new ListNode(elem % 10);
40-
var carry = (elem >= 10) ? 1 : 0;
38+
var carry = elem >= 10 ? 1 : 0;
4139
l1 = l1.next;
4240
l2 = l2.next;
43-
41+
4442
const head = number;
45-
while(l1 !== null || l2 !== null) {
43+
while (l1 !== null || l2 !== null) {
4644
var elem = carry;
47-
if(l1 !== null) {
45+
if (l1 !== null) {
4846
elem += l1.val;
4947
l1 = l1.next;
5048
}
51-
if(l2 !== null) {
49+
if (l2 !== null) {
5250
elem += l2.val;
5351
l2 = l2.next;
5452
}
55-
56-
number.next = new ListNode((elem % 10));
53+
54+
number.next = new ListNode(elem % 10);
5755
number = number.next;
58-
carry = (elem >= 10) ? 1 : 0;
56+
carry = elem >= 10 ? 1 : 0;
5957
}
60-
if(carry === 1)
61-
number.next = new ListNode(1);
58+
if (carry === 1) number.next = new ListNode(1);
6259
return head;
6360
};
6461

LeetcodeProblems/Algorithms/Award_Budget_Cuts.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,39 @@ Constraints:
2929
3030
[output] double
3131
*/
32-
var cutAwardBadges = function(nums, newBadge) {
32+
var cutAwardBadges = function (nums, newBadge) {
3333
var currentBadge = 0;
34-
for(var i = 0; i < nums.length; i++)
35-
currentBadge += nums[i];
34+
for (var i = 0; i < nums.length; i++) currentBadge += nums[i];
3635

37-
if(currentBadge < newBadge)
38-
return;
36+
if (currentBadge < newBadge) return;
3937

4038
const cap = findCap(nums, currentBadge, newBadge);
4139

4240
var iter = 0;
43-
while(iter >= 0 && nums[iter] > cap) {
41+
while (iter >= 0 && nums[iter] > cap) {
4442
nums[iter] = cap;
4543
iter++;
4644
}
4745

4846
return nums;
49-
}
47+
};
5048

51-
var findCap = function(nums, currentBadge, newBadge) {
52-
nums.sort(function(a, b) { return b - a });
53-
if(nums[nums.length - 1] * nums.length > newBadge)
49+
var findCap = function (nums, currentBadge, newBadge) {
50+
nums.sort(function (a, b) {
51+
return b - a;
52+
});
53+
if (nums[nums.length - 1] * nums.length > newBadge)
5454
return newBadge / nums.length;
5555

5656
var diff = currentBadge - newBadge;
5757
var iter = 0;
58-
while(iter < nums.length - 1 && diff > 0) {
59-
const substraction = nums[iter] - nums[iter + 1]
60-
diff -= (iter + 1) * substraction;
58+
while (iter < nums.length - 1 && diff > 0) {
59+
const substraction = nums[iter] - nums[iter + 1];
60+
diff -= (iter + 1) * substraction;
6161
iter++;
6262
}
6363

64-
return nums[iter] + (-diff) / iter;
65-
}
64+
return nums[iter] + -diff / iter;
65+
};
6666

6767
module.exports.cutAwardBadges = cutAwardBadges;

LeetcodeProblems/Algorithms/Backspace_String_Compare.js

+33-39
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,65 @@ Can you solve it in O(N) time and O(1) space?
3737
* @param {string} T
3838
* @return {boolean}
3939
*/
40-
var backspaceCompare = function(S, T) {
40+
var backspaceCompare = function (S, T) {
4141
var iterS = S.length - 1;
4242
var iterT = T.length - 1;
43-
44-
while(iterS >= 0 || iterT >= 0) {
45-
if(iterS >= 0 && S.charAt(iterS) === "#") {
43+
44+
while (iterS >= 0 || iterT >= 0) {
45+
if (iterS >= 0 && S.charAt(iterS) === "#") {
4646
var countBack = 0;
47-
while(iterS >= 0 && (countBack > 0 || S[iterS] === "#")) {
48-
if(iterS >= 0 && S[iterS] === "#") {
47+
while (iterS >= 0 && (countBack > 0 || S[iterS] === "#")) {
48+
if (iterS >= 0 && S[iterS] === "#") {
4949
countBack++;
5050
} else {
5151
countBack--;
5252
}
5353

5454
iterS--;
5555
}
56-
} else if(iterT >= 0 && T.charAt(iterT) === "#") {
57-
var countBack = 0;
58-
while(iterT >= 0 && (countBack > 0 || T[iterT] === "#")) {
59-
if(iterT >= 0 && T[iterT] === "#") {
60-
countBack++;
61-
} else {
62-
countBack--;
56+
} else if (iterT >= 0 && T.charAt(iterT) === "#") {
57+
var countBack = 0;
58+
while (iterT >= 0 && (countBack > 0 || T[iterT] === "#")) {
59+
if (iterT >= 0 && T[iterT] === "#") {
60+
countBack++;
61+
} else {
62+
countBack--;
63+
}
64+
65+
iterT--;
6366
}
64-
65-
iterT--;
66-
}
6767
} else {
68-
if(iterS < 0 || iterT < 0 || S.charAt(iterS) !== T.charAt(iterT))
68+
if (iterS < 0 || iterT < 0 || S.charAt(iterS) !== T.charAt(iterT))
6969
return false;
70-
70+
7171
iterS--;
7272
iterT--;
7373
}
7474
}
75-
76-
return iterS < 0 && iterT < 0;
75+
76+
return iterS < 0 && iterT < 0;
7777
};
7878

79-
// Naive Aproach
80-
var backspaceCompare2 = function(S, T) {
79+
// Naive Aproach
80+
var backspaceCompare2 = function (S, T) {
8181
var stackS = [];
82-
for(var i = 0; i < S.length; i++) {
83-
if(S.charAt(i) === "#")
84-
stackS.shift();
85-
else
86-
stackS.unshift(S.charAt(i));
82+
for (var i = 0; i < S.length; i++) {
83+
if (S.charAt(i) === "#") stackS.shift();
84+
else stackS.unshift(S.charAt(i));
8785
}
88-
86+
8987
var stackT = [];
90-
for(var i = 0; i < T.length; i++) {
91-
if(T.charAt(i) === "#")
92-
stackT.shift();
93-
else
94-
stackT.unshift(T.charAt(i));
88+
for (var i = 0; i < T.length; i++) {
89+
if (T.charAt(i) === "#") stackT.shift();
90+
else stackT.unshift(T.charAt(i));
9591
}
96-
97-
while(stackS.length > 0 && stackT.length > 0) {
92+
93+
while (stackS.length > 0 && stackT.length > 0) {
9894
var elemS = stackS.shift();
9995
var elemT = stackT.shift();
100-
if(elemS !== elemT)
101-
return false;
102-
96+
if (elemS !== elemT) return false;
10397
}
104-
98+
10599
return stackS.length === 0 && stackT.length === 0;
106100
};
107101

LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
3232
* @param {number[]} prices
3333
* @return {number}
3434
*/
35-
var maxProfit = function(prices) {
35+
var maxProfit = function (prices) {
3636
var profit = 0;
3737
var iter = 0;
38-
39-
while(iter < prices.length) {
40-
while(iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
38+
39+
while (iter < prices.length) {
40+
while (iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
4141
iter++;
4242
}
4343
const buy = prices[iter];
4444
iter++;
45-
while(iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
45+
while (iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
4646
iter++;
4747
}
48-
49-
if(iter < prices.length) {
48+
49+
if (iter < prices.length) {
5050
const sell = prices[iter];
5151
profit = profit + sell - buy;
5252
}
5353
}
54-
54+
5555
return profit;
5656
};
5757

58-
module.exports.maxProfit = maxProfit;
58+
module.exports.maxProfit = maxProfit;

LeetcodeProblems/Algorithms/Binary_Gap.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,19 @@ There aren't any consecutive pairs of 1's in the binary representation of 8, so
4141
* @param {number} N
4242
* @return {number}
4343
*/
44-
var binaryGap = function(N) {
44+
var binaryGap = function (N) {
4545
var maxDist = 0;
4646
var currentDist = 0;
47-
while(N > 0) {
47+
while (N > 0) {
4848
const bit = N % 2;
4949
N >>= 1;
50-
if(bit === 1) {
50+
if (bit === 1) {
5151
currentDist = 1;
52-
while(N > 0 && N % 2 === 0 ) {
52+
while (N > 0 && N % 2 === 0) {
5353
currentDist++;
54-
N >>= 1;
54+
N >>= 1;
5555
}
56-
if(N !== 0 && currentDist > maxDist)
57-
maxDist = currentDist;
56+
if (N !== 0 && currentDist > maxDist) maxDist = currentDist;
5857
}
5958
}
6059
return maxDist;

0 commit comments

Comments
 (0)