Skip to content

Commit 3a15dc4

Browse files
authored
Improved tasks 6-20
1 parent a6f5dad commit 3a15dc4

File tree

10 files changed

+113
-113
lines changed

10 files changed

+113
-113
lines changed

src/main/js/g0001_0100/s0006_zigzag_conversion/solution.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
* @param {number} numRows
66
* @return {string}
77
*/
8-
var convert = function(s, numRows) {
9-
const sLen = s.length;
8+
var convert = function (s, numRows) {
9+
const sLen = s.length
1010
if (numRows === 1) {
11-
return s;
11+
return s
1212
}
13-
const maxDist = numRows * 2 - 2;
14-
let result = "";
13+
const maxDist = numRows * 2 - 2
14+
let result = ''
1515

1616
for (let i = 0; i < numRows; i++) {
17-
let index = i;
17+
let index = i
1818
if (i === 0 || i === numRows - 1) {
1919
while (index < sLen) {
20-
result += s[index];
21-
index += maxDist;
20+
result += s[index]
21+
index += maxDist
2222
}
2323
} else {
2424
while (index < sLen) {
25-
result += s[index];
26-
index += maxDist - i * 2;
25+
result += s[index]
26+
index += maxDist - i * 2
2727
if (index >= sLen) {
28-
break;
28+
break
2929
}
30-
result += s[index];
31-
index += i * 2;
30+
result += s[index]
31+
index += i * 2
3232
}
3333
}
3434
}
35-
return result;
35+
return result
3636
}
3737

3838
export { convert }

src/main/js/g0001_0100/s0007_reverse_integer/solution.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
* @return {number}
77
*/
88
var reverse = function (x) {
9-
let r = x;
9+
let r = x
1010
if (r < 0) {
1111
r = r.toString().split('')
1212
let a = r.shift()
1313
r.reverse()
1414
r.unshift('-')
1515

1616
if (Number(r.join('')) > -2147483647) {
17-
return Number(r.join(''));
17+
return Number(r.join(''))
1818
}
1919
}
2020
let reverseN = Number(r.toString().split('').reverse().join(''))
2121
return reverseN < 2147483647 ? reverseN : 0
22-
};
22+
}
2323

2424
export { reverse }

src/main/js/g0001_0100/s0008_string_to_integer_atoi/solution.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,48 @@
44
* @param {string} s
55
* @return {number}
66
*/
7-
var myAtoi = function(str) {
7+
var myAtoi = function (str) {
88
if (str == null || str.length === 0) {
9-
return 0;
9+
return 0
1010
}
1111

12-
let i = 0;
13-
let negativeSign = false;
14-
const MAX_INT = 2147483647; // Equivalent to Integer.MAX_VALUE
15-
const MIN_INT = -2147483648; // Equivalent to Integer.MIN_VALUE
12+
let i = 0
13+
let negativeSign = false
14+
const MAX_INT = 2147483647 // Equivalent to Integer.MAX_VALUE
15+
const MIN_INT = -2147483648 // Equivalent to Integer.MIN_VALUE
1616

1717
// Skip leading whitespaces
1818
while (i < str.length && str[i] === ' ') {
19-
i++;
19+
i++
2020
}
2121

2222
if (i === str.length) {
23-
return 0;
23+
return 0
2424
}
2525

2626
// Check for optional '+' or '-' sign
2727
if (str[i] === '+') {
28-
i++;
28+
i++
2929
} else if (str[i] === '-') {
30-
i++;
31-
negativeSign = true;
30+
i++
31+
negativeSign = true
3232
}
3333

34-
let num = 0;
34+
let num = 0
3535

3636
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
37-
const digit = str[i].charCodeAt(0) - '0'.charCodeAt(0);
38-
37+
const digit = str[i].charCodeAt(0) - '0'.charCodeAt(0)
38+
3939
// Check for overflow or underflow
4040
if (num > Math.floor(MAX_INT / 10) || (num === Math.floor(MAX_INT / 10) && digit > 7)) {
41-
return negativeSign ? MIN_INT : MAX_INT;
41+
return negativeSign ? MIN_INT : MAX_INT
4242
}
4343

44-
num = num * 10 + digit;
45-
i++;
44+
num = num * 10 + digit
45+
i++
4646
}
4747

48-
return negativeSign ? -num : num;
49-
};
48+
return negativeSign ? -num : num
49+
}
5050

5151
export { myAtoi }

src/main/js/g0001_0100/s0009_palindrome_number/solution.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*/
77
var isPalindrome = function (x) {
88
if (x < 0) {
9-
return false;
9+
return false
1010
}
1111

12-
let rev = 0;
13-
let localX = x;
12+
let rev = 0
13+
let localX = x
1414

1515
while (localX > 0) {
16-
rev = rev * 10 + (localX % 10);
17-
localX = Math.floor(localX / 10);
16+
rev = rev * 10 + (localX % 10)
17+
localX = Math.floor(localX / 10)
1818
}
1919

20-
return rev === x;
21-
};
20+
return rev === x
21+
}
2222

2323
export { isPalindrome }

src/main/js/g0001_0100/s0010_regular_expression_matching/solution.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@
77
* @return {boolean}
88
*/
99
var isMatch = function (s, p) {
10-
const cache = Array.from({ length: s.length + 1 }, () => Array(p.length + 1).fill(null));
10+
const cache = Array.from({ length: s.length + 1 }, () => Array(p.length + 1).fill(null))
1111

1212
function helper(i, j) {
1313
if (j === p.length) {
14-
return i === s.length;
14+
return i === s.length
1515
}
1616

1717
if (cache[i][j] !== null) {
18-
return cache[i][j];
18+
return cache[i][j]
1919
}
2020

21-
const firstMatch = i < s.length && (s[i] === p[j] || p[j] === '.');
21+
const firstMatch = i < s.length && (s[i] === p[j] || p[j] === '.')
2222

23-
let result;
23+
let result
2424
if (j + 1 < p.length && p[j + 1] === '*') {
25-
result = (firstMatch && helper(i + 1, j)) || helper(i, j + 2);
25+
result = (firstMatch && helper(i + 1, j)) || helper(i, j + 2)
2626
} else {
27-
result = firstMatch && helper(i + 1, j + 1);
27+
result = firstMatch && helper(i + 1, j + 1)
2828
}
2929

30-
cache[i][j] = result;
31-
return result;
30+
cache[i][j] = result
31+
return result
3232
}
3333

34-
return helper(0, 0);
35-
};
34+
return helper(0, 0)
35+
}
3636

3737
export { isMatch }

src/main/js/g0001_0100/s0011_container_with_most_water/solution.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
* @return {number}
88
*/
99
var maxArea = function (height) {
10-
let maxArea = -1;
11-
let left = 0;
12-
let right = height.length - 1;
10+
let maxArea = -1
11+
let left = 0
12+
let right = height.length - 1
1313
while (left < right) {
1414
if (height[left] < height[right]) {
15-
maxArea = Math.max(maxArea, height[left] * (right - left));
16-
left++;
15+
maxArea = Math.max(maxArea, height[left] * (right - left))
16+
left++
1717
} else {
18-
maxArea = Math.max(maxArea, height[right] * (right - left));
19-
right--;
18+
maxArea = Math.max(maxArea, height[right] * (right - left))
19+
right--
2020
}
2121
}
22-
return maxArea;
23-
};
22+
return maxArea
23+
}
2424

2525
export { maxArea }

src/main/js/g0001_0100/s0015_3sum/solution.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@
77
* @return {number[][]}
88
*/
99
var threeSum = function (nums) {
10-
nums.sort((a, b) => a - b);
11-
const len = nums.length;
12-
const result = [];
10+
nums.sort((a, b) => a - b)
11+
const len = nums.length
12+
const result = []
1313

1414
for (let i = 0; i < len - 2; i++) {
15-
let l = i + 1;
16-
let r = len - 1;
15+
let l = i + 1
16+
let r = len - 1
1717

1818
while (r > l) {
19-
const sum = nums[i] + nums[l] + nums[r];
19+
const sum = nums[i] + nums[l] + nums[r]
2020

2121
if (sum < 0) {
22-
l++;
22+
l++
2323
} else if (sum > 0) {
24-
r--;
24+
r--
2525
} else {
26-
result.push([nums[i], nums[l], nums[r]]);
26+
result.push([nums[i], nums[l], nums[r]])
2727

2828
while (l < r && nums[l + 1] === nums[l]) {
29-
l++;
29+
l++
3030
}
3131

3232
while (r > l && nums[r - 1] === nums[r]) {
33-
r--;
33+
r--
3434
}
3535

36-
l++;
37-
r--;
36+
l++
37+
r--
3838
}
3939
}
4040

4141
while (i < len - 1 && nums[i + 1] === nums[i]) {
42-
i++;
42+
i++
4343
}
4444
}
4545

46-
return result;
47-
};
46+
return result
47+
}
4848

4949
export { threeSum }

src/main/js/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@
88
*/
99
var letterCombinations = function (digits) {
1010
if (digits.length === 0) {
11-
return [];
11+
return []
1212
}
1313

14-
const letters = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
15-
const result = [];
16-
const current = [];
14+
const letters = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
15+
const result = []
16+
const current = []
1717

1818
function findCombinations(index) {
1919
if (current.length === digits.length) {
20-
result.push(current.join(""));
21-
return;
20+
result.push(current.join(''))
21+
return
2222
}
2323

2424
for (let i = index; i < digits.length; i++) {
25-
const digit = parseInt(digits[i]);
26-
const letterGroup = letters[digit];
25+
const digit = parseInt(digits[i])
26+
const letterGroup = letters[digit]
2727

2828
for (const char of letterGroup) {
29-
current.push(char);
30-
findCombinations(i + 1);
31-
current.pop();
29+
current.push(char)
30+
findCombinations(i + 1)
31+
current.pop()
3232
}
3333
}
3434
}
3535

36-
findCombinations(0);
37-
return result;
38-
};
36+
findCombinations(0)
37+
return result
38+
}
3939

4040
export { letterCombinations }

src/main/js/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Big_O_Time_O(L)_Space_O(L)
33
// #2024_12_03_Time_0_ms_(100.00%)_Space_51.2_MB_(27.67%)
44

5-
import { ListNode } from "../../com_github_leetcode/listnode";
5+
import { ListNode } from '../../com_github_leetcode/listnode'
66

77
/**
88
* Definition for singly-linked list.
@@ -17,22 +17,22 @@ import { ListNode } from "../../com_github_leetcode/listnode";
1717
* @return {ListNode}
1818
*/
1919
var removeNthFromEnd = function (head, n) {
20-
let nth = n;
20+
let nth = n
2121

2222
function removeNth(node) {
2323
if (node.next === null) {
24-
return;
24+
return
2525
}
26-
removeNth(node.next);
27-
nth--;
26+
removeNth(node.next)
27+
nth--
2828
if (nth === 0) {
29-
node.next = node.next.next;
29+
node.next = node.next.next
3030
}
3131
}
3232

33-
const dummy = new ListNode(0, head);
34-
removeNth(dummy);
35-
return dummy.next;
36-
};
33+
const dummy = new ListNode(0, head)
34+
removeNth(dummy)
35+
return dummy.next
36+
}
3737

3838
export { removeNthFromEnd }

0 commit comments

Comments
 (0)