Skip to content

Commit 1b9c80c

Browse files
authored
Added tasks 36-117
1 parent 0ad37ed commit 1b9c80c

File tree

38 files changed

+1086
-0
lines changed

38 files changed

+1086
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package s0086_partition_list
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
86\. Partition List
2+
3+
Medium
4+
5+
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.
6+
7+
You should **preserve** the original relative order of the nodes in each of the two partitions.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg)
12+
13+
**Input:** head = [1,4,3,2,5,2], x = 3
14+
15+
**Output:** [1,2,2,4,3,5]
16+
17+
**Example 2:**
18+
19+
**Input:** head = [2,1], x = 2
20+
21+
**Output:** [1,2]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 200]`.
26+
* `-100 <= Node.val <= 100`
27+
* `-200 <= x <= 200`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package s0086_partition_list
2+
3+
// #Medium #Two_Pointers #Linked_List #Top_Interview_150_Linked_List
4+
// #2025_05_17_Time_0_ms_(100.00%)_Space_4.30_MB_(62.68%)
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* type ListNode struct {
9+
* Val int
10+
* Next *ListNode
11+
* }
12+
*/
13+
func partition(head *ListNode, x int) *ListNode {
14+
nHead := &ListNode{Val: 0}
15+
nTail := &ListNode{Val: 0}
16+
ptr := nTail
17+
temp := nHead
18+
for head != nil {
19+
nNext := head.Next
20+
if head.Val < x {
21+
nHead.Next = head
22+
nHead = nHead.Next
23+
} else {
24+
nTail.Next = head
25+
nTail = nTail.Next
26+
}
27+
head = nNext
28+
}
29+
nTail.Next = nil
30+
nHead.Next = ptr.Next
31+
return temp.Next
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package s0086_partition_list
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestPartition(t *testing.T) {
9+
head := &ListNode{Val: 1, Next: &ListNode{Val: 4, Next: &ListNode{Val: 3, Next: &ListNode{Val: 2, Next: &ListNode{Val: 5, Next: &ListNode{Val: 2}}}}}}
10+
result := partition(head, 3)
11+
assert.Equal(t, 1, result.Val)
12+
assert.Equal(t, 2, result.Next.Val)
13+
assert.Equal(t, 2, result.Next.Next.Val)
14+
assert.Equal(t, 4, result.Next.Next.Next.Val)
15+
assert.Equal(t, 3, result.Next.Next.Next.Next.Val)
16+
assert.Equal(t, 5, result.Next.Next.Next.Next.Next.Val)
17+
}
18+
19+
func TestPartition2(t *testing.T) {
20+
head := &ListNode{Val: 2, Next: &ListNode{Val: 1}}
21+
result := partition(head, 2)
22+
assert.Equal(t, 1, result.Val)
23+
assert.Equal(t, 2, result.Next.Val)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
88\. Merge Sorted Array
2+
3+
Easy
4+
5+
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.
6+
7+
**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.
8+
9+
The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
14+
15+
**Output:** [1,2,2,3,5,6]
16+
17+
**Explanation:** The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [<ins>1</ins>,<ins>2</ins>,2,<ins>3</ins>,5,6] with the underlined elements coming from nums1.
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1], m = 1, nums2 = [], n = 0
22+
23+
**Output:** [1]
24+
25+
**Explanation:** The arrays we are merging are [1] and []. The result of the merge is [1].
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [0], m = 0, nums2 = [1], n = 1
30+
31+
**Output:** [1]
32+
33+
**Explanation:** The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
34+
35+
**Constraints:**
36+
37+
* `nums1.length == m + n`
38+
* `nums2.length == n`
39+
* `0 <= m, n <= 200`
40+
* `1 <= m + n <= 200`
41+
* <code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code>
42+
43+
**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package s0088_merge_sorted_array
2+
3+
// #Easy #Top_Interview_Questions #Array #Sorting #Two_Pointers #Data_Structure_I_Day_2_Array
4+
// #Top_Interview_150_Array/String #2025_05_17_Time_0_ms_(100.00%)_Space_4.22_MB_(38.99%)
5+
6+
func merge(nums1 []int, m int, nums2 []int, n int) {
7+
i := m - 1
8+
j := len(nums1) - 1
9+
p2 := n - 1
10+
for p2 >= 0 {
11+
if i >= 0 && nums1[i] > nums2[p2] {
12+
nums1[j] = nums1[i]
13+
j--
14+
i--
15+
} else {
16+
nums1[j] = nums2[p2]
17+
j--
18+
p2--
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package s0088_merge_sorted_array
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMerge(t *testing.T) {
9+
nums1 := []int{1, 2, 3, 0, 0, 0}
10+
nums2 := []int{2, 5, 6}
11+
merge(nums1, 3, nums2, 3)
12+
assert.Equal(t, []int{1, 2, 2, 3, 5, 6}, nums1)
13+
}
14+
15+
func TestMerge2(t *testing.T) {
16+
nums1 := []int{1}
17+
nums2 := []int{}
18+
merge(nums1, 1, nums2, 0)
19+
assert.Equal(t, []int{1}, nums1)
20+
}
21+
22+
func TestMerge3(t *testing.T) {
23+
nums1 := []int{0}
24+
nums2 := []int{1}
25+
merge(nums1, 0, nums2, 1)
26+
assert.Equal(t, []int{1}, nums1)
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package s0092_reverse_linked_list_ii
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
92\. Reverse Linked List II
2+
3+
Medium
4+
5+
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], left = 2, right = 4
12+
13+
**Output:** [1,4,3,2,5]
14+
15+
**Example 2:**
16+
17+
**Input:** head = [5], left = 1, right = 1
18+
19+
**Output:** [5]
20+
21+
**Constraints:**
22+
23+
* The number of nodes in the list is `n`.
24+
* `1 <= n <= 500`
25+
* `-500 <= Node.val <= 500`
26+
* `1 <= left <= right <= n`
27+
28+
**Follow up:** Could you do it in one pass?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package s0092_reverse_linked_list_ii
2+
3+
// #Medium #Linked_List #Top_Interview_150_Linked_List
4+
// #2025_05_17_Time_0_ms_(100.00%)_Space_4.09_MB_(56.36%)
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* type ListNode struct {
9+
* Val int
10+
* Next *ListNode
11+
* }
12+
*/
13+
func reverseBetween(head *ListNode, left int, right int) *ListNode {
14+
if left == right {
15+
return head
16+
}
17+
var prev *ListNode
18+
temp := head
19+
var start *ListNode
20+
k := left
21+
for temp != nil && k > 1 {
22+
prev = temp
23+
temp = temp.Next
24+
k--
25+
}
26+
if left > 1 && prev != nil {
27+
prev.Next = nil
28+
}
29+
var prev1 *ListNode
30+
start = temp
31+
for temp != nil && right-left >= 0 {
32+
prev1 = temp
33+
temp = temp.Next
34+
right--
35+
}
36+
if prev1 != nil {
37+
prev1.Next = nil
38+
}
39+
if left > 1 && prev != nil {
40+
prev.Next = reverse(start)
41+
} else {
42+
head = reverse(start)
43+
prev = head
44+
}
45+
for prev.Next != nil {
46+
prev = prev.Next
47+
}
48+
prev.Next = temp
49+
return head
50+
}
51+
52+
func reverse(head *ListNode) *ListNode {
53+
var p, q, r *ListNode
54+
p = head
55+
for p != nil {
56+
q = p.Next
57+
p.Next = r
58+
r = p
59+
p = q
60+
}
61+
return r
62+
}
63+
64+
func decode(s string) string {
65+
return s
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0092_reverse_linked_list_ii
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestReverseBetween(t *testing.T) {
9+
head := &ListNode{Val: 1}
10+
head.Next = &ListNode{Val: 2}
11+
head.Next.Next = &ListNode{Val: 3}
12+
head.Next.Next.Next = &ListNode{Val: 4}
13+
head.Next.Next.Next.Next = &ListNode{Val: 5}
14+
15+
result := reverseBetween(head, 2, 4)
16+
assert.Equal(t, 1, result.Val)
17+
assert.Equal(t, 4, result.Next.Val)
18+
assert.Equal(t, 3, result.Next.Next.Val)
19+
assert.Equal(t, 2, result.Next.Next.Next.Val)
20+
assert.Equal(t, 5, result.Next.Next.Next.Next.Val)
21+
}
22+
23+
func TestReverseBetween2(t *testing.T) {
24+
head := &ListNode{Val: 5}
25+
26+
result := reverseBetween(head, 1, 1)
27+
assert.Equal(t, 5, result.Val)
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
97\. Interleaving String
2+
3+
Medium
4+
5+
Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
6+
7+
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` **non-empty** substrings respectively, such that:
8+
9+
* <code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code>
10+
* <code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code>
11+
* `|n - m| <= 1`
12+
* The **interleaving** is <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> or <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code>
13+
14+
**Note:** `a + b` is the concatenation of strings `a` and `b`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg)
19+
20+
**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
21+
22+
**Output:** true
23+
24+
**Explanation:** One way to obtain s3 is: Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a". Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac". Since s3 can be obtained by interleaving s1 and s2, we return true.
25+
26+
**Example 2:**
27+
28+
**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
29+
30+
**Output:** false
31+
32+
**Explanation:** Notice how it is impossible to interleave s2 with any other string to obtain s3.
33+
34+
**Example 3:**
35+
36+
**Input:** s1 = "", s2 = "", s3 = ""
37+
38+
**Output:** true
39+
40+
**Constraints:**
41+
42+
* `0 <= s1.length, s2.length <= 100`
43+
* `0 <= s3.length <= 200`
44+
* `s1`, `s2`, and `s3` consist of lowercase English letters.
45+
46+
**Follow up:** Could you solve it using only `O(s2.length)` additional memory space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package s0097_interleaving_string
2+
3+
// #Medium #String #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
4+
// #2025_05_17_Time_0_ms_(100.00%)_Space_4.85_MB_(28.57%)
5+
6+
func isInterleave(s1 string, s2 string, s3 string) bool {
7+
if len(s3) != len(s1)+len(s2) {
8+
return false
9+
}
10+
cache := make([][]*bool, len(s1)+1)
11+
for i := range cache {
12+
cache[i] = make([]*bool, len(s2)+1)
13+
}
14+
return isInterleaveHelper(s1, s2, s3, 0, 0, 0, cache)
15+
}
16+
17+
func isInterleaveHelper(s1, s2, s3 string, i1, i2, i3 int, cache [][]*bool) bool {
18+
if cache[i1][i2] != nil {
19+
return *cache[i1][i2]
20+
}
21+
if i1 == len(s1) && i2 == len(s2) && i3 == len(s3) {
22+
return true
23+
}
24+
result := false
25+
if i1 < len(s1) && s1[i1] == s3[i3] {
26+
result = isInterleaveHelper(s1, s2, s3, i1+1, i2, i3+1, cache)
27+
}
28+
if i2 < len(s2) && s2[i2] == s3[i3] {
29+
result = result || isInterleaveHelper(s1, s2, s3, i1, i2+1, i3+1, cache)
30+
}
31+
cache[i1][i2] = &result
32+
return result
33+
}

0 commit comments

Comments
 (0)