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

+5-7
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

+5-11
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

+2-5
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

-3
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

-3
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

-1
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

+2-2
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

-1
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

-7
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

+2-2
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

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

-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ var maxSubArray = function(nums) {
4848
let sum = 0
4949

5050
for (const num of nums) {
51-
// Calculate subarray sum
5251
sum += num
5352
maxi = Math.max(sum, maxi)
5453
if (sum < 0) {
55-
// Reset sum if it's negative
5654
sum = 0
5755
}
5856
}

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

-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end
3737
* @return {number[][]}
3838
*/
3939
var merge = function(intervals) {
40-
// Sort intervals based on the starting points
4140
intervals.sort((a, b) => a[0] - b[0])
4241

4342
const result = []
@@ -46,10 +45,8 @@ var merge = function(intervals) {
4645

4746
for (const next of intervals) {
4847
if (current[1] >= next[0]) {
49-
// Merge intervals
5048
current[1] = Math.max(current[1], next[1])
5149
} else {
52-
// Move to the next interval
5350
current = next;
5451
result.push(current)
5552
}

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

-4
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,21 @@ The test cases are generated so that the answer will be less than or equal to <c
4646
* @return {number}
4747
*/
4848
var uniquePaths = function(m, n) {
49-
// Initialize a 2D array with all values set to 0
5049
const dp = Array.from({ length: m }, () => Array(n).fill(0))
5150

52-
// Fill the first row and first column with 1
5351
for (let i = 0; i < m; i++) {
5452
dp[i][0] = 1
5553
}
5654
for (let j = 0; j < n; j++) {
5755
dp[0][j] = 1
5856
}
5957

60-
// Fill the rest of the dp table
6158
for (let i = 1; i < m; i++) {
6259
for (let j = 1; j < n; j++) {
6360
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
6461
}
6562
}
6663

67-
// The answer is in the bottom-right corner
6864
return dp[m - 1][n - 1]
6965
};
7066

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

-6
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,24 @@ var minPathSum = function(grid) {
4343
const rows = grid.length
4444
const cols = grid[0].length
4545

46-
// Handle the special case where grid has only one cell
4746
if (rows === 1 && cols === 1) {
4847
return grid[0][0]
4948
}
5049

51-
// Create a 2D array for dynamic programming
5250
const dm = Array.from({ length: rows }, () => Array(cols).fill(0))
5351

54-
// Initialize the last column
5552
let s = 0
5653
for (let r = rows - 1; r >= 0; r--) {
5754
dm[r][cols - 1] = grid[r][cols - 1] + s
5855
s += grid[r][cols - 1]
5956
}
6057

61-
// Initialize the last row
6258
s = 0
6359
for (let c = cols - 1; c >= 0; c--) {
6460
dm[rows - 1][c] = grid[rows - 1][c] + s
6561
s += grid[rows - 1][c]
6662
}
6763

68-
// Recursive helper function
6964
const recur = (r, c) => {
7065
if (
7166
dm[r][c] === 0 &&
@@ -82,7 +77,6 @@ var minPathSum = function(grid) {
8277
return dm[r][c]
8378
}
8479

85-
// Start recursion from the top-left corner
8680
return recur(0, 0)
8781
};
8882

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

-4
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,15 @@ var climbStairs = function(n) {
5151
return n
5252
}
5353

54-
// Create a cache (DP array) to store results
5554
const cache = new Array(n)
5655

57-
// Initialize base cases
5856
cache[0] = 1
5957
cache[1] = 2
6058

61-
// Fill the cache using the recurrence relation
6259
for (let i = 2; i < n; i++) {
6360
cache[i] = cache[i - 1] + cache[i - 2]
6461
}
6562

66-
// Return the result for the nth step
6763
return cache[n - 1]
6864
};
6965

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

-3
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,15 @@ var minDistance = function(w1, w2) {
6262
const n1 = w1.length
6363
const n2 = w2.length
6464

65-
// Ensure the longer word is always w1
6665
if (n2 > n1) {
6766
return minDistance(w2, w1)
6867
}
6968

70-
// Initialize the dp array
7169
const dp = new Array(n2 + 1).fill(0)
7270
for (let j = 0; j <= n2; j++) {
7371
dp[j] = j
7472
}
7573

76-
// Compute minimum distance
7774
for (let i = 1; i <= n1; i++) {
7875
let pre = dp[0]
7976
dp[0] = i

0 commit comments

Comments
 (0)