Skip to content

Commit b6e972b

Browse files
authored
Added js tasks
1 parent 61f6630 commit b6e972b

File tree

72 files changed

+3055
-415
lines changed

Some content is hidden

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

72 files changed

+3055
-415
lines changed

README.md

Lines changed: 415 additions & 415 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ListNode {
2+
constructor(val, next) {
3+
this.val = val === undefined ? 0 : val
4+
this.next = next === undefined ? null : next
5+
}
6+
7+
toString() {
8+
let result = `${this.val}`
9+
let current = this.next
10+
while (current !== null) {
11+
result += `, ${current.val}`
12+
current = current.next
13+
}
14+
return result
15+
}
16+
}
17+
18+
export { ListNode }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
// #AI_can_be_used_to_solve_the_task #2024_11_17_Time_1_ms_(89.15%)_Space_51.9_MB_(13.71%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} target
8+
* @return {number[]}
9+
*/
10+
function twoSum(nums, target) {
11+
const indexMap = new Map()
12+
for (let i = 0; i < nums.length; i++) {
13+
const requiredNum = target - nums[i]
14+
if (indexMap.has(requiredNum)) {
15+
return [indexMap.get(requiredNum), i]
16+
}
17+
indexMap.set(nums[i], i)
18+
}
19+
return [-1, -1]
20+
}
21+
22+
export { twoSum }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
4+
// #2024_11_29_Time_3_ms_(81.61%)_Space_55.3_MB_(96.39%)
5+
6+
import { ListNode } from 'src/main/js/com_github_leetcode/listnode'
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* @param {ListNode} l1
17+
* @param {ListNode} l2
18+
* @return {ListNode}
19+
*/
20+
var addTwoNumbers = function (l1, l2) {
21+
const dummyHead = new ListNode(0)
22+
let p = l1,
23+
q = l2,
24+
curr = dummyHead
25+
let carry = 0
26+
27+
while (p !== null || q !== null) {
28+
const x = p !== null ? p.val : 0
29+
const y = q !== null ? q.val : 0
30+
const sum = carry + x + y
31+
carry = Math.floor(sum / 10)
32+
curr.next = new ListNode(sum % 10)
33+
curr = curr.next
34+
35+
if (p !== null) p = p.next
36+
if (q !== null) q = q.next
37+
}
38+
39+
if (carry > 0) {
40+
curr.next = new ListNode(carry)
41+
}
42+
43+
return dummyHead.next
44+
}
45+
46+
export { addTwoNumbers }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
// #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
4+
// #2024_11_29_Time_3_ms_(98.96%)_Space_53.9_MB_(69.91%)
5+
6+
/**
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
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
15+
16+
for (let i = 0; i < s.length; i++) {
17+
const cur = s.charCodeAt(i) // Get ASCII code of the current character
18+
19+
if (lastIndices[cur] < start) {
20+
// If the character hasn't been seen in the current substring
21+
lastIndices[cur] = i
22+
curLen++
23+
} else {
24+
// If the character was seen, update the start position
25+
const lastIndex = lastIndices[cur]
26+
start = lastIndex + 1
27+
curLen = i - start + 1
28+
lastIndices[cur] = i
29+
}
30+
31+
maxLen = Math.max(maxLen, curLen)
32+
}
33+
34+
return maxLen
35+
}
36+
37+
export { lengthOfLongestSubstring }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #AI_can_be_used_to_solve_the_task
3+
// #2024_11_29_Time_3_ms_(91.90%)_Space_54.1_MB_(88.03%)
4+
5+
/**
6+
* @param {number[]} nums1
7+
* @param {number[]} nums2
8+
* @return {number}
9+
*/
10+
var findMedianSortedArrays = function (nums1, nums2) {
11+
if (nums2.length < nums1.length) {
12+
return findMedianSortedArrays(nums2, nums1)
13+
}
14+
15+
let n1 = nums1.length,
16+
n2 = nums2.length
17+
let low = 0,
18+
high = n1
19+
20+
while (low <= high) {
21+
let cut1 = Math.floor((low + high) / 2)
22+
let cut2 = Math.floor((n1 + n2 + 1) / 2) - cut1
23+
24+
let l1 = cut1 === 0 ? -Infinity : nums1[cut1 - 1]
25+
let l2 = cut2 === 0 ? -Infinity : nums2[cut2 - 1]
26+
let r1 = cut1 === n1 ? Infinity : nums1[cut1]
27+
let r2 = cut2 === n2 ? Infinity : nums2[cut2]
28+
29+
if (l1 <= r2 && l2 <= r1) {
30+
if ((n1 + n2) % 2 === 0) {
31+
return (Math.max(l1, l2) + Math.min(r1, r2)) / 2.0
32+
}
33+
return Math.max(l1, l2)
34+
} else if (l1 > r2) {
35+
high = cut1 - 1
36+
} else {
37+
low = cut1 + 1
38+
}
39+
}
40+
41+
return 0.0
42+
}
43+
44+
export { findMedianSortedArrays }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest palindromic substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab" **Note:** "aba" is also a valid answer.
12+
13+
**Example 2:**
14+
15+
**Input:** s = "cbbd"
16+
17+
**Output:** "bb"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "a"
22+
23+
**Output:** "a"
24+
25+
**Example 4:**
26+
27+
**Input:** s = "ac"
28+
29+
**Output:** "a"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consist of only digits and English letters.

0 commit comments

Comments
 (0)