Skip to content

Commit c1a17f2

Browse files
committed
Improved js tasks
1 parent 077ae47 commit c1a17f2

File tree

43 files changed

+38
-163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+38
-163
lines changed

src/main/js/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88
* @return {number}
99
*/
1010
var lengthOfLongestSubstring = function (s) {
11-
const lastIndices = new Array(256).fill(-1) // Array to store last indices of characters
12-
let maxLen = 0 // Tracks maximum length of substring
13-
let curLen = 0 // Current substring length
14-
let start = 0 // Start index of the current substring
11+
const lastIndices = new Array(256).fill(-1)
12+
let maxLen = 0
13+
let curLen = 0
14+
let start = 0
1515

1616
for (let i = 0; i < s.length; i++) {
17-
const cur = s.charCodeAt(i) // Get ASCII code of the current character
17+
const cur = s.charCodeAt(i)
1818

1919
if (lastIndices[cur] < start) {
20-
// If the character hasn't been seen in the current substring
2120
lastIndices[cur] = i
2221
curLen++
2322
} else {
24-
// If the character was seen, update the start position
2523
const lastIndex = lastIndices[cur]
2624
start = lastIndex + 1
2725
curLen = i - start + 1

src/main/js/g0001_0100/s0005_longest_palindromic_substring/solution.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,36 @@
88
* @return {string}
99
*/
1010
var longestPalindrome = function (s) {
11-
// Create the transformed string with '#' characters
1211
const newStr = new Array(s.length * 2 + 1).fill('#')
1312
for (let i = 0; i < s.length; i++) {
1413
newStr[2 * i + 1] = s[i]
1514
}
1615

17-
const dp = new Array(newStr.length).fill(0) // Array to store radius of palindromes
18-
let friendCenter = 0 // Center of the current known palindrome
19-
let friendRadius = 0 // Radius of the current known palindrome
20-
let lpsCenter = 0 // Center of the longest palindrome
21-
let lpsRadius = 0 // Radius of the longest palindrome
16+
const dp = new Array(newStr.length).fill(0)
17+
let friendCenter = 0
18+
let friendRadius = 0
19+
let lpsCenter = 0
20+
let lpsRadius = 0
2221

2322
for (let i = 0; i < newStr.length; i++) {
24-
// Calculate initial radius
2523
dp[i] =
2624
friendCenter + friendRadius > i ? Math.min(dp[2 * friendCenter - i], friendCenter + friendRadius - i) : 1
2725

28-
// Expand the palindrome around the current center
2926
while (i + dp[i] < newStr.length && i - dp[i] >= 0 && newStr[i + dp[i]] === newStr[i - dp[i]]) {
3027
dp[i]++
3128
}
3229

33-
// Update the friend palindrome if needed
3430
if (friendCenter + friendRadius < i + dp[i]) {
3531
friendCenter = i
3632
friendRadius = dp[i]
3733
}
3834

39-
// Update the longest palindrome if needed
4035
if (lpsRadius < dp[i]) {
4136
lpsCenter = i
4237
lpsRadius = dp[i]
4338
}
4439
}
4540

46-
// Extract the longest palindrome substring
4741
const start = Math.floor((lpsCenter - lpsRadius + 1) / 2)
4842
const end = Math.floor((lpsCenter + lpsRadius - 1) / 2)
4943
return s.substring(start, end)

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ var myAtoi = function (str) {
1111

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

17-
// Skip leading whitespaces
1817
while (i < str.length && str[i] === ' ') {
1918
i++
2019
}
@@ -23,7 +22,6 @@ var myAtoi = function (str) {
2322
return 0
2423
}
2524

26-
// Check for optional '+' or '-' sign
2725
if (str[i] === '+') {
2826
i++
2927
} else if (str[i] === '-') {
@@ -36,7 +34,6 @@ var myAtoi = function (str) {
3634
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
3735
const digit = str[i].charCodeAt(0) - '0'.charCodeAt(0)
3836

39-
// Check for overflow or underflow
4037
if (num > Math.floor(MAX_INT / 10) || (num === Math.floor(MAX_INT / 10) && digit > 7)) {
4138
return negativeSign ? MIN_INT : MAX_INT
4239
}

src/main/js/g0001_0100/s0025_reverse_nodes_in_k_group/solution.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const reverseKGroup = function (head, k) {
2222
let len = head
2323
let count = 0
2424

25-
// Check if there are at least k nodes to reverse
2625
while (count < k) {
2726
if (len === null) {
2827
return head
@@ -31,7 +30,6 @@ const reverseKGroup = function (head, k) {
3130
count++
3231
}
3332

34-
// Reverse the first k nodes
3533
let current = head
3634
let next = null
3735
let prev = null
@@ -45,7 +43,6 @@ const reverseKGroup = function (head, k) {
4543
i++
4644
}
4745

48-
// Recursively reverse the next groups and connect the lists
4946
head.next = reverseKGroup(next, k)
5047

5148
return prev

src/main/js/g0001_0100/s0031_next_permutation/solution.js

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

1313
let i = nums.length - 2
1414

15-
// Find the first index `i` where nums[i] < nums[i + 1]
1615
while (i >= 0 && nums[i] >= nums[i + 1]) {
1716
i--
1817
}
1918

2019
if (i >= 0) {
21-
// Find the smallest number larger than nums[i] to swap with
2220
let j = nums.length - 1
2321
while (nums[j] <= nums[i]) {
2422
j--
2523
}
2624
swap(nums, i, j)
2725
}
2826

29-
// Reverse the portion of the array from index `i + 1` to the end
3027
reverse(nums, i + 1, nums.length - 1)
3128
};
3229

src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var search = function(nums, target) {
1717
return mid
1818
}
1919
if (nums[lo] <= nums[mid]) {
20-
// Target is in the sorted left half
2120
if (nums[lo] <= target && target <= nums[mid]) {
2221
hi = mid - 1
2322
} else {

src/main/js/g0001_0100/s0039_combination_sum/solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ var combinationSum = function(candidates, target) {
1414
const combinationSumRec = (n, candidates, target, subList, ans) => {
1515
if (target === 0 || n === 0) {
1616
if (target === 0) {
17-
ans.push([...subList]) // Create a copy of subList
17+
ans.push([...subList])
1818
}
1919
return
2020
}
2121

2222
if (target - candidates[n - 1] >= 0) {
2323
subList.push(candidates[n - 1])
2424
combinationSumRec(n, candidates, target - candidates[n - 1], subList, ans)
25-
subList.pop() // Backtracking step
25+
subList.pop()
2626
}
2727

2828
combinationSumRec(n - 1, candidates, target, subList, ans)

src/main/js/g0001_0100/s0041_first_missing_positive/solution.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var firstMissingPositive = function(nums) {
1212
nums[i] <= nums.length &&
1313
nums[nums[i] - 1] !== nums[i]
1414
) {
15-
// Swap nums[i] with nums[nums[i] - 1]
1615
let temp = nums[nums[i] - 1]
1716
nums[nums[i] - 1] = nums[i]
1817
nums[i] = temp

src/main/js/g0001_0100/s0042_trapping_rain_water/solution.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,13 @@ var trap = function(height) {
1616
let lVal = height[l]
1717
let rVal = height[r]
1818

19-
// Determine the lower wall
2019
if (lVal < rVal) {
21-
// Update the lower wall based on the left pointer
2220
lowerWall = Math.max(lVal, lowerWall)
23-
// Add water trapped at the current position
2421
res += lowerWall - lVal
25-
// Move the left pointer
2622
l++
2723
} else {
28-
// Update the lower wall based on the right pointer
2924
lowerWall = Math.max(rVal, lowerWall)
30-
// Add water trapped at the current position
3125
res += lowerWall - rVal
32-
// Move the right pointer
3326
r--
3427
}
3528
}

src/main/js/g0001_0100/s0046_permutations/solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var permute = function(nums) {
1717

1818
const permuteRecur = (nums, finalResult, currResult, used) => {
1919
if (currResult.length === nums.length) {
20-
finalResult.push([...currResult]) // Create a copy of currResult
20+
finalResult.push([...currResult])
2121
return
2222
}
2323
for (let i = 0; i < nums.length; i++) {
@@ -28,7 +28,7 @@ var permute = function(nums) {
2828
used[i] = true
2929
permuteRecur(nums, finalResult, currResult, used)
3030
used[i] = false
31-
currResult.pop() // Backtrack
31+
currResult.pop()
3232
}
3333
}
3434

0 commit comments

Comments
 (0)