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

Commit 36fa023

Browse files
Apply prettier formatting
1 parent 34c6755 commit 36fa023

8 files changed

+136
-133
lines changed

‎0001_twoSum.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* Time O(n) - one iteration of n elements.
99
*/
1010
const twoSum = (nums, target) => {
11-
const hash = {};
12-
let index, remaining;
11+
const hash = {};
12+
let index, remaining;
1313

14-
for (index = 0; index < nums.length; index++) {
15-
remaining = target - nums[index];
14+
for (index = 0; index < nums.length; index++) {
15+
remaining = target - nums[index];
1616

17-
if (hash[remaining] !== undefined) {
18-
return [hash[remaining], index];
19-
}
20-
21-
hash[nums[index]] = index;
17+
if (hash[remaining] !== undefined) {
18+
return [hash[remaining], index];
2219
}
20+
21+
hash[nums[index]] = index;
22+
}
2323
};

‎0002_addTwoNumbers.js‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@
1717
* Time O(n) - Where n represents number of digits in bigger number.
1818
*/
1919
const addTwoNumbers = (l1, l2) => {
20-
let head = new ListNode(0), current, value = 0;
20+
let head = new ListNode(0),
21+
current,
22+
value = 0;
2123

22-
current = head;
23-
while (l2 || l1 || value) {
24-
if (l1) {
25-
value += l1.val;
26-
l1 = l1.next;
27-
}
24+
current = head;
25+
while (l2 || l1 || value) {
26+
if (l1) {
27+
value += l1.val;
28+
l1 = l1.next;
29+
}
2830

29-
if (l2) {
30-
value += l2.val;
31-
l2 = l2.next;
32-
}
31+
if (l2) {
32+
value += l2.val;
33+
l2 = l2.next;
34+
}
3335

34-
current.val = value % 10;
35-
value = value - 10 >= 0 ? 1 : 0;
36+
current.val = value % 10;
37+
value = value - 10 >= 0 ? 1 : 0;
3638

37-
if (l1 || l2 || value) {
38-
current.next = new ListNode(0);
39-
current = current.next;
40-
}
39+
if (l1 || l2 || value) {
40+
current.next = new ListNode(0);
41+
current = current.next;
4142
}
43+
}
4244

43-
return head;
45+
return head;
4446
};
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
const lengthOfLongestSubstring = (s) => {
2-
let longest = 0, current = 0, hashMap = {};
1+
const lengthOfLongestSubstring = s => {
2+
let longest = 0,
3+
current = 0,
4+
hashMap = {};
35

4-
for (let index = 0; index < s.length; index++) {
5-
if (!hashMap[s[index]]) {
6-
hashMap[s[index]] = true;
7-
current++;
8-
} else {
9-
if (current > longest) {
10-
longest = current;
11-
}
6+
for (let index = 0; index < s.length; index++) {
7+
if (!hashMap[s[index]]) {
8+
hashMap[s[index]] = true;
9+
current++;
10+
} else {
11+
if (current > longest) {
12+
longest = current;
13+
}
1214

13-
current = 0;
14-
index--;
15-
hashMap = {};
16-
}
15+
current = 0;
16+
index--;
17+
hashMap = {};
18+
}
1719

18-
if (current > longest) {
19-
longest = current;
20-
}
20+
if (current > longest) {
21+
longest = current;
2122
}
23+
}
2224

23-
return longest;
25+
return longest;
2426
};

‎0020_validParentheses.js‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
* Space O(n) - put up to n characters on stack.
77
* Time O(n) - iterate over n characters of input.
88
*/
9-
var isValid = function(s) {
10-
const stack = [];
11-
12-
for (let index = 0; index < s.length; index++) {
13-
let char = s[index];
14-
15-
if (['{', '(', '['].includes(char)) {
16-
stack.push(char);
17-
} else {
18-
const top = stack.pop();
19-
20-
if (!top) return false;
21-
if (char === ')' && !(top === '(')) return false;
22-
if (char === ']' && !(top === '[')) return false;
23-
if (char === '}' && !(top === '{')) return false;
24-
}
9+
const isValid = s => {
10+
const stack = [];
11+
12+
for (let index = 0; index < s.length; index++) {
13+
let char = s[index];
14+
15+
if (['{', '(', '['].includes(char)) {
16+
stack.push(char);
17+
} else {
18+
const top = stack.pop();
19+
20+
if (!top) return false;
21+
if (char === ')' && !(top === '(')) return false;
22+
if (char === ']' && !(top === '[')) return false;
23+
if (char === '}' && !(top === '{')) return false;
2524
}
26-
27-
return !stack.length;
28-
};
25+
}
26+
27+
return !stack.length;
28+
};

‎0021_mergeTwoSortedLists.js‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
* Time O(n) - iterate until shorter list is fully merged in (up two m+n operations).
1818
*/
1919
const mergeTwoLists = (l1, l2) => {
20-
const start = new ListNode(0);
21-
let prev = start;
20+
const start = new ListNode(0);
21+
let prev = start;
2222

23-
while (l1 && l2) {
24-
if (l1.val < l2.val) {
25-
prev.next = l1;
26-
l1 = l1.next;
27-
} else {
28-
prev.next = l2;
29-
l2 = l2.next;
30-
}
31-
32-
prev = prev.next;
23+
while (l1 && l2) {
24+
if (l1.val < l2.val) {
25+
prev.next = l1;
26+
l1 = l1.next;
27+
} else {
28+
prev.next = l2;
29+
l2 = l2.next;
3330
}
3431

35-
prev.next = !l1 ? l2 : l1;
32+
prev = prev.next;
33+
}
34+
35+
prev.next = !l1 ? l2 : l1;
3636

37-
return start.next;
38-
};
37+
return start.next;
38+
};

‎0023_mergeKSortedLists.js‎

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
* Time O(nlogk) - logk pairings, each call to mergeTwoLists is n.
1717
*/
1818
const mergeKLists = lists => {
19-
if (!lists.length) return null;
20-
if (lists.length === 1) return lists[0];
19+
if (!lists.length) return null;
20+
if (lists.length === 1) return lists[0];
2121

22-
const k = lists.length;
23-
let pairings = 1;
22+
const k = lists.length;
23+
let pairings = 1;
2424

25-
while (pairings < k) {
26-
for (let index = 0; index < k - pairings; index += pairings * 2) {
27-
lists[index] = mergeTwoLists(lists[index], lists[index + pairings]);
28-
}
29-
pairings *= 2;
25+
while (pairings < k) {
26+
for (let index = 0; index < k - pairings; index += pairings * 2) {
27+
lists[index] = mergeTwoLists(lists[index], lists[index + pairings]);
3028
}
29+
pairings *= 2;
30+
}
3131

32-
return lists[0];
32+
return lists[0];
3333
};
3434

3535
/**
@@ -39,23 +39,22 @@ const mergeKLists = lists => {
3939
* @description Merge two sorted linked lists and return it as a new sorted list.
4040
*/
4141
const mergeTwoLists = (l1, l2) => {
42-
const head = new ListNode(0);
43-
44-
let current = head;
45-
while (l1 && l2) {
46-
if (l1.val < l2.val) {
47-
current.next = l1;
48-
l1 = l1.next;
49-
} else {
50-
current.next = l2;
51-
l2 = l2.next;
52-
}
53-
54-
current = current.next;
42+
const head = new ListNode(0);
43+
44+
let current = head;
45+
while (l1 && l2) {
46+
if (l1.val < l2.val) {
47+
current.next = l1;
48+
l1 = l1.next;
49+
} else {
50+
current.next = l2;
51+
l2 = l2.next;
5552
}
5653

57-
current.next = !l1 ? l2 : l1;
54+
current = current.next;
55+
}
5856

59-
return head.next;
60-
};
57+
current.next = !l1 ? l2 : l1;
6158

59+
return head.next;
60+
};

‎0056_mergeIntervals.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* Time O(nlogn) - sort (logn) + one iteration of n elements. Depends on js engine sort implementation though.
88
*/
99
const merge = intervals => {
10-
intervals.sort((a, b) => a[0] > b[0] ? 1 : -1);
10+
intervals.sort((a, b) => (a[0] > b[0] ? 1 : -1));
1111

12-
const merged = [];
12+
const merged = [];
1313

14-
intervals.forEach(interval => {
15-
if (!merged.length || merged[merged.length - 1][1] < interval[0]) merged.push(interval);
16-
else merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]);
17-
})
14+
intervals.forEach(interval => {
15+
if (!merged.length || merged[merged.length - 1][1] < interval[0]) merged.push(interval);
16+
else merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]);
17+
});
1818

19-
return merged;
20-
};
19+
return merged;
20+
};

‎0680_validPalindromeII.js‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
* Space O(1) - no extra memory used.
77
* Time O(n) - iterate once over a list, perform up to two check (each is n).
88
*/
9-
const validPalindrome = word => {
10-
const isPalindrome = (word, left, right) => {
11-
while (left < right) {
12-
if (word[left] !== word[right]) return false;
13-
14-
left++;
15-
right--;
16-
}
17-
18-
return true;
9+
const validPalindrome = word => {
10+
const isPalindrome = (word, left, right) => {
11+
while (left < right) {
12+
if (word[left] !== word[right]) return false;
13+
14+
left++;
15+
right--;
1916
}
2017

21-
letleft=0;
22-
letright=word.length-1;
18+
returntrue;
19+
};
2320

24-
while (left < right) {
25-
if (word[left] !== word[right]) {
26-
return isPalindrome(word, left + 1, right) || isPalindrome(word, left, right - 1);
27-
}
21+
let left = 0;
22+
let right = word.length - 1;
2823

29-
left++;
30-
right--;
24+
while (left < right) {
25+
if (word[left] !== word[right]) {
26+
return isPalindrome(word, left + 1, right) || isPalindrome(word, left, right - 1);
3127
}
3228

33-
return true;
34-
};
29+
left++;
30+
right--;
31+
}
32+
33+
return true;
34+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /