Skip to content

Commit 4cf9b67

Browse files
authored
Added tasks 86-100
1 parent 36af8e5 commit 4cf9b67

File tree

15 files changed

+545
-0
lines changed

15 files changed

+545
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace LeetCodeNet.G0001_0100.S0086_partition_list {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
private ListNode BuildList(int[] vals) {
8+
ListNode dummy = new ListNode(0);
9+
ListNode curr = dummy;
10+
foreach (var v in vals) {
11+
curr.next = new ListNode(v);
12+
curr = curr.next;
13+
}
14+
return dummy.next;
15+
}
16+
17+
private int[] ToArray(ListNode head) {
18+
var list = new List<int>();
19+
while (head != null) {
20+
list.Add(head.val);
21+
head = head.next;
22+
}
23+
return list.ToArray();
24+
}
25+
26+
[Fact]
27+
public void Partition() {
28+
var solution = new Solution();
29+
var head = BuildList(new int[] {1,4,3,2,5,2});
30+
var result = solution.Partition(head, 3);
31+
Assert.Equal(new int[] {1,2,2,4,3,5}, ToArray(result));
32+
}
33+
34+
[Fact]
35+
public void Partition2() {
36+
var solution = new Solution();
37+
var head = BuildList(new int[] {2,1});
38+
var result = solution.Partition(head, 2);
39+
Assert.Equal(new int[] {1,2}, ToArray(result));
40+
}
41+
42+
[Fact]
43+
public void Partition3() {
44+
var solution = new Solution();
45+
var head = BuildList(new int[] {1,2,3});
46+
var result = solution.Partition(head, 5);
47+
Assert.Equal(new int[] {1,2,3}, ToArray(result));
48+
}
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace LeetCodeNet.G0001_0100.S0088_merge_sorted_array {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Merge() {
8+
var solution = new Solution();
9+
int[] nums1 = {1,2,3,0,0,0};
10+
int[] nums2 = {2,5,6};
11+
solution.Merge(nums1, 3, nums2, 3);
12+
Assert.Equal(new int[] {1,2,2,3,5,6}, nums1);
13+
}
14+
15+
[Fact]
16+
public void Merge2() {
17+
var solution = new Solution();
18+
int[] nums1 = {1};
19+
int[] nums2 = {};
20+
solution.Merge(nums1, 1, nums2, 0);
21+
Assert.Equal(new int[] {1}, nums1);
22+
}
23+
24+
[Fact]
25+
public void Merge3() {
26+
var solution = new Solution();
27+
int[] nums1 = {0};
28+
int[] nums2 = {1};
29+
solution.Merge(nums1, 0, nums2, 1);
30+
Assert.Equal(new int[] {1}, nums1);
31+
}
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace LeetCodeNet.G0001_0100.S0092_reverse_linked_list_ii {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
private ListNode BuildList(int[] vals) {
8+
ListNode dummy = new ListNode(0);
9+
ListNode curr = dummy;
10+
foreach (var v in vals) {
11+
curr.next = new ListNode(v);
12+
curr = curr.next;
13+
}
14+
return dummy.next;
15+
}
16+
17+
private int[] ToArray(ListNode head) {
18+
var list = new System.Collections.Generic.List<int>();
19+
while (head != null) {
20+
list.Add(head.val);
21+
head = head.next;
22+
}
23+
return list.ToArray();
24+
}
25+
26+
[Fact]
27+
public void ReverseBetween() {
28+
var solution = new Solution();
29+
var head = BuildList(new int[] {1,2,3,4,5});
30+
var result = solution.ReverseBetween(head, 2, 4);
31+
Assert.Equal(new int[] {1,4,3,2,5}, ToArray(result));
32+
}
33+
34+
[Fact]
35+
public void ReverseBetween2() {
36+
var solution = new Solution();
37+
var head = BuildList(new int[] {5});
38+
var result = solution.ReverseBetween(head, 1, 1);
39+
Assert.Equal(new int[] {5}, ToArray(result));
40+
}
41+
42+
[Fact]
43+
public void ReverseBetween3() {
44+
var solution = new Solution();
45+
var head = BuildList(new int[] {1,2,3});
46+
var result = solution.ReverseBetween(head, 1, 3);
47+
Assert.Equal(new int[] {3,2,1}, ToArray(result));
48+
}
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0001_0100.S0097_interleaving_string {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void IsInterleave() {
8+
var solution = new Solution();
9+
Assert.True(solution.IsInterleave("aabcc", "dbbca", "aadbbcbcac"));
10+
}
11+
12+
[Fact]
13+
public void IsInterleave2() {
14+
var solution = new Solution();
15+
Assert.False(solution.IsInterleave("aabcc", "dbbca", "aadbbbaccc"));
16+
}
17+
18+
[Fact]
19+
public void IsInterleave3() {
20+
var solution = new Solution();
21+
Assert.True(solution.IsInterleave("", "", ""));
22+
}
23+
24+
[Fact]
25+
public void IsInterleave4() {
26+
var solution = new Solution();
27+
Assert.False(solution.IsInterleave("a", "b", "abc"));
28+
}
29+
}
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace LeetCodeNet.G0001_0100.S0100_same_tree {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
private TreeNode BuildTree(int?[] vals, int index = 0) {
8+
if (index >= vals.Length || vals[index] == null) return null;
9+
TreeNode root = new TreeNode(vals[index].Value);
10+
root.left = BuildTree(vals, 2 * index + 1);
11+
root.right = BuildTree(vals, 2 * index + 2);
12+
return root;
13+
}
14+
15+
[Fact]
16+
public void IsSameTree() {
17+
var solution = new Solution();
18+
var p = BuildTree(new int?[] {1, 2, 3});
19+
var q = BuildTree(new int?[] {1, 2, 3});
20+
Assert.True(solution.IsSameTree(p, q));
21+
}
22+
23+
[Fact]
24+
public void IsSameTree2() {
25+
var solution = new Solution();
26+
var p = BuildTree(new int?[] {1, 2});
27+
var q = BuildTree(new int?[] {1, null, 2});
28+
Assert.False(solution.IsSameTree(p, q));
29+
}
30+
31+
[Fact]
32+
public void IsSameTree3() {
33+
var solution = new Solution();
34+
var p = BuildTree(new int?[] {1, 2, 1});
35+
var q = BuildTree(new int?[] {1, 1, 2});
36+
Assert.False(solution.IsSameTree(p, q));
37+
}
38+
39+
[Fact]
40+
public void IsSameTree4() {
41+
var solution = new Solution();
42+
Assert.True(solution.IsSameTree(null, null));
43+
}
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace LeetCodeNet.G0001_0100.S0086_partition_list {
2+
3+
// #Medium #Two_Pointers #Linked_List #Top_Interview_150_Linked_List
4+
// #2025_07_05_Time_0_ms_(100.00%)_Space_42.69_MB_(64.54%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* public int val;
12+
* public ListNode next;
13+
* public ListNode(int val=0, ListNode next=null) {
14+
* this.val = val;
15+
* this.next = next;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public ListNode Partition(ListNode head, int x) {
21+
ListNode beforeHead = new ListNode(0);
22+
ListNode afterHead = new ListNode(0);
23+
ListNode before = beforeHead;
24+
ListNode after = afterHead;
25+
while (head != null) {
26+
if (head.val < x) {
27+
before.next = head;
28+
before = before.next;
29+
} else {
30+
after.next = head;
31+
after = after.next;
32+
}
33+
head = head.next;
34+
}
35+
after.next = null;
36+
before.next = afterHead.next;
37+
return beforeHead.next;
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
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`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0001_0100.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_07_05_Time_0_ms_(100.00%)_Space_46.88_MB_(90.46%)
5+
6+
public class Solution {
7+
public void Merge(int[] nums1, int m, int[] nums2, int n) {
8+
int i = m - 1;
9+
int j = n - 1;
10+
int k = m + n - 1;
11+
while (i >= 0 && j >= 0) {
12+
if (nums1[i] > nums2[j]) {
13+
nums1[k--] = nums1[i--];
14+
} else {
15+
nums1[k--] = nums2[j--];
16+
}
17+
}
18+
while (j >= 0) {
19+
nums1[k--] = nums2[j--];
20+
}
21+
}
22+
}
23+
}
Lines changed: 43 additions & 0 deletions
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 [1,2,2,3,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?
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace LeetCodeNet.G0001_0100.S0092_reverse_linked_list_ii {
2+
3+
// #Medium #Linked_List #Top_Interview_150_Linked_List
4+
// #2025_07_05_Time_0_ms_(100.00%)_Space_41.68_MB_(85.47%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* public int val;
12+
* public ListNode next;
13+
* public ListNode(int val=0, ListNode next=null) {
14+
* this.val = val;
15+
* this.next = next;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public ListNode ReverseBetween(ListNode head, int left, int right) {
21+
if (head == null || left == right) {
22+
return head;
23+
}
24+
ListNode dummy = new ListNode(0, head);
25+
ListNode prev = dummy;
26+
for (int i = 0; i < left - 1; i++) {
27+
prev = prev.next;
28+
}
29+
ListNode curr = prev.next;
30+
for (int i = 0; i < right - left; i++) {
31+
ListNode next = curr.next;
32+
curr.next = next.next;
33+
next.next = prev.next;
34+
prev.next = next;
35+
}
36+
return dummy.next;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)