Skip to content

Commit 6801d1f

Browse files
committed
Improved js tasks
1 parent 35d5f64 commit 6801d1f

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/readme.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,18 @@ Given a string `s`, find the length of the **longest substring** without repeati
5050
* @return {number}
5151
*/
5252
var lengthOfLongestSubstring = function (s) {
53-
const lastIndices = new Array(256).fill(-1) // Array to store last indices of characters
54-
let maxLen = 0 // Tracks maximum length of substring
55-
let curLen = 0 // Current substring length
56-
let start = 0 // Start index of the current substring
53+
const lastIndices = new Array(256).fill(-1)
54+
let maxLen = 0
55+
let curLen = 0
56+
let start = 0
5757

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

6161
if (lastIndices[cur] < start) {
62-
// If the character hasn't been seen in the current substring
6362
lastIndices[cur] = i
6463
curLen++
6564
} else {
66-
// If the character was seen, update the start position
6765
const lastIndex = lastIndices[cur]
6866
start = lastIndex + 1
6967
curLen = i - start + 1

src/main/js/g0001_0100/s0005_longest_palindromic_substring/readme.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,36 @@ Given a string `s`, return _the longest palindromic substring_ in `s`.
4444
* @return {string}
4545
*/
4646
var longestPalindrome = function (s) {
47-
// Create the transformed string with '#' characters
4847
const newStr = new Array(s.length * 2 + 1).fill('#')
4948
for (let i = 0; i < s.length; i++) {
5049
newStr[2 * i + 1] = s[i]
5150
}
5251

53-
const dp = new Array(newStr.length).fill(0) // Array to store radius of palindromes
54-
let friendCenter = 0 // Center of the current known palindrome
55-
let friendRadius = 0 // Radius of the current known palindrome
56-
let lpsCenter = 0 // Center of the longest palindrome
57-
let lpsRadius = 0 // Radius of the longest palindrome
52+
const dp = new Array(newStr.length).fill(0)
53+
let friendCenter = 0
54+
let friendRadius = 0
55+
let lpsCenter = 0
56+
let lpsRadius = 0
5857

5958
for (let i = 0; i < newStr.length; i++) {
60-
// Calculate initial radius
6159
dp[i] =
6260
friendCenter + friendRadius > i ? Math.min(dp[2 * friendCenter - i], friendCenter + friendRadius - i) : 1
6361

64-
// Expand the palindrome around the current center
6562
while (i + dp[i] < newStr.length && i - dp[i] >= 0 && newStr[i + dp[i]] === newStr[i - dp[i]]) {
6663
dp[i]++
6764
}
6865

69-
// Update the friend palindrome if needed
7066
if (friendCenter + friendRadius < i + dp[i]) {
7167
friendCenter = i
7268
friendRadius = dp[i]
7369
}
7470

75-
// Update the longest palindrome if needed
7671
if (lpsRadius < dp[i]) {
7772
lpsCenter = i
7873
lpsRadius = dp[i]
7974
}
8075
}
8176

82-
// Extract the longest palindrome substring
8377
const start = Math.floor((lpsCenter - lpsRadius + 1) / 2)
8478
const end = Math.floor((lpsCenter + lpsRadius - 1) / 2)
8579
return s.substring(start, end)

src/main/js/g0001_0100/s0008_string_to_integer_atoi/readme.md

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

130130
let i = 0
131131
let negativeSign = false
132-
const MAX_INT = 2147483647 // Equivalent to Integer.MAX_VALUE
133-
const MIN_INT = -2147483648 // Equivalent to Integer.MIN_VALUE
132+
const MAX_INT = 2147483647
133+
const MIN_INT = -2147483648
134134

135-
// Skip leading whitespaces
136135
while (i < str.length && str[i] === ' ') {
137136
i++
138137
}
@@ -141,7 +140,6 @@ var myAtoi = function (str) {
141140
return 0
142141
}
143142

144-
// Check for optional '+' or '-' sign
145143
if (str[i] === '+') {
146144
i++
147145
} else if (str[i] === '-') {
@@ -154,7 +152,6 @@ var myAtoi = function (str) {
154152
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
155153
const digit = str[i].charCodeAt(0) - '0'.charCodeAt(0)
156154

157-
// Check for overflow or underflow
158155
if (num > Math.floor(MAX_INT / 10) || (num === Math.floor(MAX_INT / 10) && digit > 7)) {
159156
return negativeSign ? MIN_INT : MAX_INT
160157
}

src/main/js/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const reverseKGroup = function (head, k) {
7171
let len = head
7272
let count = 0
7373

74-
// Check if there are at least k nodes to reverse
7574
while (count < k) {
7675
if (len === null) {
7776
return head
@@ -80,7 +79,6 @@ const reverseKGroup = function (head, k) {
8079
count++
8180
}
8281

83-
// Reverse the first k nodes
8482
let current = head
8583
let next = null
8684
let prev = null
@@ -94,7 +92,6 @@ const reverseKGroup = function (head, k) {
9492
i++
9593
}
9694

97-
// Recursively reverse the next groups and connect the lists
9895
head.next = reverseKGroup(next, k)
9996

10097
return prev

src/main/js/g0001_0100/s0031_next_permutation/readme.md

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

5757
let i = nums.length - 2
5858

59-
// Find the first index `i` where nums[i] < nums[i + 1]
6059
while (i >= 0 && nums[i] >= nums[i + 1]) {
6160
i--
6261
}
6362

6463
if (i >= 0) {
65-
// Find the smallest number larger than nums[i] to swap with
6664
let j = nums.length - 1
6765
while (nums[j] <= nums[i]) {
6866
j--
6967
}
7068
swap(nums, i, j)
7169
}
7270

73-
// Reverse the portion of the array from index `i + 1` to the end
7471
reverse(nums, i + 1, nums.length - 1)
7572
};
7673

src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ var search = function(nums, target) {
5656
return mid
5757
}
5858
if (nums[lo] <= nums[mid]) {
59-
// Target is in the sorted left half
6059
if (nums[lo] <= target && target <= nums[mid]) {
6160
hi = mid - 1
6261
} else {

src/main/js/g0001_0100/s0039_combination_sum/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ var combinationSum = function(candidates, target) {
5858
const combinationSumRec = (n, candidates, target, subList, ans) => {
5959
if (target === 0 || n === 0) {
6060
if (target === 0) {
61-
ans.push([...subList]) // Create a copy of subList
61+
ans.push([...subList])
6262
}
6363
return
6464
}
6565

6666
if (target - candidates[n - 1] >= 0) {
6767
subList.push(candidates[n - 1])
6868
combinationSumRec(n, candidates, target - candidates[n - 1], subList, ans)
69-
subList.pop() // Backtracking step
69+
subList.pop()
7070
}
7171

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

src/main/js/g0001_0100/s0041_first_missing_positive/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ var firstMissingPositive = function(nums) {
5252
nums[i] <= nums.length &&
5353
nums[nums[i] - 1] !== nums[i]
5454
) {
55-
// Swap nums[i] with nums[nums[i] - 1]
5655
let temp = nums[nums[i] - 1]
5756
nums[nums[i] - 1] = nums[i]
5857
nums[i] = temp

src/main/js/g0001_0100/s0042_trapping_rain_water/readme.md

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

49-
// Determine the lower wall
5049
if (lVal < rVal) {
51-
// Update the lower wall based on the left pointer
5250
lowerWall = Math.max(lVal, lowerWall)
53-
// Add water trapped at the current position
5451
res += lowerWall - lVal
55-
// Move the left pointer
5652
l++
5753
} else {
58-
// Update the lower wall based on the right pointer
5954
lowerWall = Math.max(rVal, lowerWall)
60-
// Add water trapped at the current position
6155
res += lowerWall - rVal
62-
// Move the right pointer
6356
r--
6457
}
6558
}

src/main/js/g0001_0100/s0046_permutations/readme.md

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

4949
const permuteRecur = (nums, finalResult, currResult, used) => {
5050
if (currResult.length === nums.length) {
51-
finalResult.push([...currResult]) // Create a copy of currResult
51+
finalResult.push([...currResult])
5252
return
5353
}
5454
for (let i = 0; i < nums.length; i++) {
@@ -59,7 +59,7 @@ var permute = function(nums) {
5959
used[i] = true
6060
permuteRecur(nums, finalResult, currResult, used)
6161
used[i] = false
62-
currResult.pop() // Backtrack
62+
currResult.pop()
6363
}
6464
}
6565

0 commit comments

Comments
 (0)