Skip to content

Commit fe6a6fd

Browse files
authored
Added tasks 206-238
1 parent 603db53 commit fe6a6fd

File tree

36 files changed

+1001
-6
lines changed

36 files changed

+1001
-6
lines changed

LeetCodeNet.Tests/G0101_0200/S0102_binary_tree_level_order_traversal/SolutionTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace LeetCodeNet.G0101_0200.S0102_binary_tree_level_order_traversal {
22

33
using Xunit;
4-
using Com_github_leetcode;
4+
using LeetCodeNet.Com_github_leetcode;
55

66
public class SolutionTest {
77
[Fact]

LeetCodeNet.Tests/G0101_0200/S0104_maximum_depth_of_binary_tree/SolutionTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace LeetCodeNet.G0101_0200.S0104_maximum_depth_of_binary_tree {
22

33
using Xunit;
4-
using Com_github_leetcode;
4+
using LeetCodeNet.Com_github_leetcode;
55

66
public class SolutionTest {
77
[Fact]

LeetCodeNet.Tests/G0101_0200/S0124_binary_tree_maximum_path_sum/SolutionTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace LeetCodeNet.G0101_0200.S0124_binary_tree_maximum_path_sum {
22

33
using Xunit;
4-
using Com_github_leetcode;
4+
using LeetCodeNet.Com_github_leetcode;
55

66
public class SolutionTest {
77
[Fact]

LeetCodeNet.Tests/G0101_0200/S0142_linked_list_cycle_ii/SolutionTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void DetectCycle2() {
2525
[Fact]
2626
public void DetectCycle3() {
2727
ListNode listNode1 = new ListNode(1);
28-
Assert.Equal(null, new Solution().DetectCycle(listNode1));
28+
Assert.Null(new Solution().DetectCycle(listNode1));
2929
}
3030
}
3131
}

LeetCodeNet.Tests/G0101_0200/S0148_sort_list/SolutionTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void SortList2() {
2525

2626
[Fact]
2727
public void SortList3() {
28-
Assert.Equal(null, new Solution().SortList(null));
28+
Assert.Null(new Solution().SortList(null));
2929
}
3030
}
3131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LeetCodeNet.G0201_0300.S0206_reverse_linked_list {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void ReverseList() {
9+
ListNode headActual = new ListNode(1);
10+
headActual.next = new ListNode(2);
11+
headActual.next.next = new ListNode(3);
12+
headActual.next.next.next = new ListNode(4);
13+
headActual.next.next.next.next = new ListNode(5);
14+
Assert.Equal("5, 4, 3, 2, 1", new Solution().ReverseList(headActual).ToString());
15+
}
16+
17+
[Fact]
18+
public void ReverseList2() {
19+
ListNode headActual = new ListNode(1);
20+
headActual.next = new ListNode(2);
21+
Assert.Equal("2, 1", new Solution().ReverseList(headActual).ToString());
22+
}
23+
24+
[Fact]
25+
public void ReverseList3() {
26+
Assert.Null(new Solution().ReverseList(null));
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0201_0300.S0207_course_schedule {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void CanFinish() {
8+
Assert.True(new Solution().CanFinish(2, new int[][] { new int[] { 1, 0 } }));
9+
}
10+
11+
[Fact]
12+
public void CanFinish2() {
13+
Assert.False(new Solution().CanFinish(2, new int[][] { new int[] { 1, 0 }, new int[] { 0, 1 } }));
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0208_implement_trie_prefix_tree {
2+
3+
using Xunit;
4+
5+
public class TrieTest {
6+
[Fact]
7+
public void Trie() {
8+
Trie trie = new Trie();
9+
trie.Insert("apple");
10+
// return True
11+
Assert.True(trie.Search("apple"));
12+
// return False
13+
Assert.False(trie.Search("app"));
14+
// return True
15+
Assert.True(trie.StartsWith("app"));
16+
trie.Insert("app");
17+
// return True
18+
Assert.True(trie.Search("app"));
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0201_0300.S0215_kth_largest_element_in_an_array {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void FindKthLargest() {
8+
Assert.Equal(5, new Solution().FindKthLargest(new int[] { 3, 2, 1, 5, 6, 4 }, 2));
9+
}
10+
11+
[Fact]
12+
public void FindKthLargest2() {
13+
Assert.Equal(4, new Solution().FindKthLargest(new int[] { 3, 2, 3, 1, 2, 4, 5, 5, 6 }, 4));
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LeetCodeNet.G0201_0300.S0221_maximal_square {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaximalSquare() {
8+
char[][] input = {
9+
new[] {'1', '0', '1', '0', '0'},
10+
new[] {'1', '0', '1', '1', '1'},
11+
new[] {'1', '1', '1', '1', '1'},
12+
new[] {'1', '0', '0', '1', '0'}
13+
};
14+
Assert.Equal(4, new Solution().MaximalSquare(input));
15+
}
16+
17+
[Fact]
18+
public void MaximalSquare2() {
19+
char[][] input = { new[] {'0', '1'}, new[] {'1', '0'} };
20+
Assert.Equal(1, new Solution().MaximalSquare(input));
21+
}
22+
23+
[Fact]
24+
public void MaximalSquare3() {
25+
char[][] input = { new[] {'0'} };
26+
Assert.Equal(0, new Solution().MaximalSquare(input));
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0201_0300.S0226_invert_binary_tree {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void InvertTree() {
9+
TreeNode root = TreeUtils.ConstructBinaryTree(new List<int?> { 4, 2, 7, 1, 3, 6, 9 });
10+
Assert.Equal("4,7,9,6,2,3,1", new Solution().InvertTree(root).ToString());
11+
}
12+
13+
[Fact]
14+
public void InvertTree2() {
15+
TreeNode root = TreeUtils.ConstructBinaryTree(new List<int?> { 2, 1, 3 });
16+
Assert.Equal("2,3,1", new Solution().InvertTree(root).ToString());
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0201_0300.S0230_kth_smallest_element_in_a_bst {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void KthSmallest() {
9+
TreeNode root = TreeNode.Create(new List<int?> { 3, 1, 4, null, 2 });
10+
Assert.Equal(1, new Solution().KthSmallest(root, 1));
11+
}
12+
13+
[Fact]
14+
public void KthSmallest2() {
15+
TreeNode root = TreeNode.Create(new List<int?> { 5, 3, 6, 2, 4, null, null, 1 });
16+
Assert.Equal(3, new Solution().KthSmallest(root, 3));
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0201_0300.S0234_palindrome_linked_list {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void IsPalindrome() {
9+
ListNode headActual = new ListNode(1);
10+
headActual.next = new ListNode(2);
11+
headActual.next.next = new ListNode(2);
12+
headActual.next.next.next = new ListNode(1);
13+
Assert.True(new Solution().IsPalindrome(headActual));
14+
}
15+
16+
[Fact]
17+
public void IsPalindrome2() {
18+
ListNode headActual = new ListNode(1);
19+
headActual.next = new ListNode(2);
20+
Assert.False(new Solution().IsPalindrome(headActual));
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LeetCodeNet.G0201_0300.S0236_lowest_common_ancestor_of_a_binary_tree {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void LowestCommonAncestor() {
9+
TreeNode leftNodeLeftNode = new TreeNode(6);
10+
TreeNode leftNodeRightNode = new TreeNode(2, new TreeNode(7), new TreeNode(4));
11+
TreeNode leftNode = new TreeNode(5, leftNodeLeftNode, leftNodeRightNode);
12+
TreeNode rightNode = new TreeNode(1, new TreeNode(0), new TreeNode(8));
13+
TreeNode root = new TreeNode(3, leftNode, rightNode);
14+
Assert.Equal(3, new Solution().LowestCommonAncestor(root, new TreeNode(5), new TreeNode(1)).val);
15+
}
16+
17+
[Fact]
18+
public void LowestCommonAncestor2() {
19+
TreeNode leftNodeLeftNode = new TreeNode(6);
20+
TreeNode leftNodeRightNode = new TreeNode(2, new TreeNode(7), new TreeNode(4));
21+
TreeNode leftNode = new TreeNode(5, leftNodeLeftNode, leftNodeRightNode);
22+
TreeNode rightNode = new TreeNode(1, new TreeNode(0), new TreeNode(8));
23+
TreeNode root = new TreeNode(3, leftNode, rightNode);
24+
Assert.Equal(5, new Solution().LowestCommonAncestor(root, new TreeNode(5), new TreeNode(4)).val);
25+
}
26+
27+
[Fact]
28+
public void LowestCommonAncestor3() {
29+
Assert.Equal(2,
30+
new Solution().LowestCommonAncestor(
31+
new TreeNode(2, new TreeNode(1), null),
32+
new TreeNode(2),
33+
new TreeNode(1)).val);
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace LeetCodeNet.G0201_0300.S0238_product_of_array_except_self {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void ProductExceptSelf() {
8+
Assert.Equal(
9+
new int[] { 24, 12, 8, 6 },
10+
new Solution().ProductExceptSelf(new int[] { 1, 2, 3, 4 })
11+
);
12+
}
13+
14+
[Fact]
15+
public void ProductExceptSelf2() {
16+
Assert.Equal(
17+
new int[] { 0, 0, 9, 0, 0 },
18+
new Solution().ProductExceptSelf(new int[] { -1, 1, 0, -3, 3 })
19+
);
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LeetCodeNet.G0201_0300.S0206_reverse_linked_list {
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
4+
// #Data_Structure_I_Day_8_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking
5+
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
6+
// #2024_01_10_Time_57_ms_(95.02%)_Space_40.9_MB_(19.99%)
7+
8+
using LeetCodeNet.Com_github_leetcode;
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* public int val;
14+
* public ListNode next;
15+
* public ListNode(int val=0, ListNode next=null) {
16+
* this.val = val;
17+
* this.next = next;
18+
* }
19+
* }
20+
*/
21+
public class Solution {
22+
public ListNode ReverseList(ListNode head) {
23+
ListNode prev = null;
24+
ListNode curr = head;
25+
while (curr != null) {
26+
ListNode next = curr.next;
27+
curr.next = prev;
28+
prev = curr;
29+
curr = next;
30+
}
31+
return prev;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
206\. Reverse Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5]
12+
13+
**Output:** [5,4,3,2,1]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
18+
19+
**Input:** head = [1,2]
20+
21+
**Output:** [2,1]
22+
23+
**Example 3:**
24+
25+
**Input:** head = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is the range `[0, 5000]`.
32+
* `-5000 <= Node.val <= 5000`
33+
34+
**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?

0 commit comments

Comments
 (0)