forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,034 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace LeetCodeNet.Com_github_leetcode { | ||
using System.Collections.Generic; | ||
|
||
public static class ArrayUtils { | ||
public static List<List<int>> GetLists(int[][] expected) { | ||
List<List<int>> expectedList = new List<List<int>>(); | ||
foreach (int[] value in expected) { | ||
List<int> expectedItem = new List<int>(); | ||
expectedList.Add(expectedItem); | ||
foreach (int item in value) { | ||
expectedItem.Add(item); | ||
} | ||
} | ||
return expectedList; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
LeetCodeNet.Tests/G0001_0100/S0011_container_with_most_water/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace LeetCodeNet.G0001_0100.S0011_container_with_most_water { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MaxArea() { | ||
Assert.Equal(49, new Solution().MaxArea(new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7 })); | ||
} | ||
|
||
[Fact] | ||
public void MaxArea2() { | ||
Assert.Equal(1, new Solution().MaxArea(new int[] { 1, 1 })); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace LeetCodeNet.G0001_0100.S0015_3sum { | ||
|
||
using Xunit; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void ThreeSum() { | ||
Assert.Equal( | ||
ArrayUtils.GetLists(new int[][] { new int[] { -1, -1, 2 }, new int[] { -1, 0, 1 } }), | ||
new Solution().ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 }) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ThreeSum2() { | ||
Assert.Equal( | ||
ArrayUtils.GetLists(new int[][] { }), | ||
new Solution().ThreeSum(new int[] { }) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ThreeSum3() { | ||
Assert.Equal( | ||
ArrayUtils.GetLists(new int[][] { }), | ||
new Solution().ThreeSum(new int[] { 0 }) | ||
); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
LeetCodeNet.Tests/G0001_0100/S0017_letter_combinations_of_a_phone_number/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace LeetCodeNet.G0001_0100.S0017_letter_combinations_of_a_phone_number { | ||
|
||
using Xunit; | ||
using System.Collections.Generic; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void LetterCombinations() { | ||
Assert.Equal( | ||
new List<string> { "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" }, | ||
new Solution().LetterCombinations("23") | ||
); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations2() { | ||
Assert.Empty(new Solution().LetterCombinations("")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations3() { | ||
Assert.Equal(new List<string> { "a", "b", "c" }, new Solution().LetterCombinations("2")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations4() { | ||
Assert.Equal(new List<string> { "g", "h", "i" }, new Solution().LetterCombinations("4")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations5() { | ||
Assert.Equal(new List<string> { "j", "k", "l" }, new Solution().LetterCombinations("5")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations6() { | ||
Assert.Equal(new List<string> { "m", "n", "o" }, new Solution().LetterCombinations("6")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations7() { | ||
Assert.Equal(new List<string> { "p", "q", "r", "s" }, new Solution().LetterCombinations("7")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations8() { | ||
Assert.Equal(new List<string> { "t", "u", "v" }, new Solution().LetterCombinations("8")); | ||
} | ||
|
||
[Fact] | ||
public void LetterCombinations9() { | ||
Assert.Equal(new List<string> { "w", "x", "y", "z" }, new Solution().LetterCombinations("9")); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
LeetCodeNet.Tests/G0001_0100/S0019_remove_nth_node_from_end_of_list/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace LeetCodeNet.G0001_0100.S0019_remove_nth_node_from_end_of_list { | ||
|
||
using Xunit; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void RemoveNthFromEnd() { | ||
ListNode node1 = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 }); | ||
Assert.Equal("1, 2, 3, 5", new Solution().RemoveNthFromEnd(node1, 2).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void RemoveNthFromEnd2() { | ||
ListNode node1 = new ListNode(1); | ||
Assert.Null(new Solution().RemoveNthFromEnd(node1, 1)); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
LeetCodeNet.Tests/G0001_0100/S0020_valid_parentheses/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace LeetCodeNet.G0001_0100.S0020_valid_parentheses { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void IsValid() { | ||
Assert.True(new Solution().IsValid("()")); | ||
} | ||
|
||
[Fact] | ||
public void IsValid2() { | ||
Assert.True(new Solution().IsValid("()[]{}")); | ||
} | ||
|
||
[Fact] | ||
public void IsValid3() { | ||
Assert.False(new Solution().IsValid("(]")); | ||
} | ||
|
||
[Fact] | ||
public void IsValid4() { | ||
Assert.False(new Solution().IsValid("([)]")); | ||
} | ||
|
||
[Fact] | ||
public void IsValid5() { | ||
Assert.True(new Solution().IsValid("{[]}")); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
LeetCodeNet.Tests/G0001_0100/S0021_merge_two_sorted_lists/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace LeetCodeNet.G0001_0100.S0021_merge_two_sorted_lists { | ||
|
||
using Xunit; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MergeTwoLists() { | ||
ListNode l1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 2, 4 }); | ||
ListNode l2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 4 }); | ||
Assert.Equal("1, 1, 2, 3, 4, 4", new Solution().MergeTwoLists(l1, l2).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void MergeTwoLists2() { | ||
Assert.Equal("0, 0", new Solution().MergeTwoLists(new ListNode(), new ListNode()).ToString()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
LeetCodeNet.Tests/G0001_0100/S0022_generate_parentheses/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace LeetCodeNet.G0001_0100.S0022_generate_parentheses { | ||
|
||
using Xunit; | ||
using System.Collections.Generic; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void GenerateParenthesis() { | ||
Assert.Equal( | ||
new List<string> { "((()))", "(()())", "(())()", "()(())", "()()()" }, | ||
new Solution().GenerateParenthesis(3) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void GenerateParenthesis2() { | ||
Assert.Equal( | ||
new List<string> { "()" }, | ||
new Solution().GenerateParenthesis(1) | ||
); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
LeetCodeNet.Tests/G0001_0100/S0023_merge_k_sorted_lists/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace LeetCodeNet.G0001_0100.S0023_merge_k_sorted_lists { | ||
|
||
using Xunit; | ||
using System.Collections.Generic; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MergeKLists() { | ||
ListNode head1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 4, 5 }); | ||
ListNode head2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 4 }); | ||
ListNode head3 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 2, 6 }); | ||
Assert.Equal( | ||
"1, 1, 2, 3, 4, 4, 5, 6", | ||
new Solution().MergeKLists(new ListNode[] { head1, head2, head3 }).ToString() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void MergeKLists2() { | ||
ListNode head1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 5, 7, 11 }); | ||
ListNode head2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 2, 8, 12 }); | ||
ListNode head3 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 4, 6, 9, 10 }); | ||
Assert.Equal( | ||
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", | ||
new Solution().MergeKLists(new ListNode[] { head1, head2, head3 }).ToString() | ||
); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
LeetCodeNet.Tests/G0001_0100/S0024_swap_nodes_in_pairs/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace LeetCodeNet.G0001_0100.S0024_swap_nodes_in_pairs { | ||
|
||
using Xunit; | ||
using System.Collections.Generic; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void SwapPairs() { | ||
ListNode head = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 2, 3, 4 }); | ||
Assert.Equal("2, 1, 4, 3", new Solution().SwapPairs(head).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void SwapPairs2() { | ||
Assert.Equal("1", new Solution().SwapPairs(new ListNode(1)).ToString()); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
LeetCodeNet.Tests/G0001_0100/S0025_reverse_nodes_in_k_group/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace LeetCodeNet.G0001_0100.S0025_reverse_nodes_in_k_group { | ||
|
||
using Xunit; | ||
using System.Collections.Generic; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void ReverseKGroup() { | ||
ListNode head = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 }); | ||
Assert.Equal("2, 1, 4, 3, 5", new Solution().ReverseKGroup(head, 2).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void ReverseKGroup2() { | ||
ListNode head = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 }); | ||
Assert.Equal("3, 2, 1, 4, 5", new Solution().ReverseKGroup(head, 3).ToString()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
LeetCodeNet/G0001_0100/S0011_container_with_most_water/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace LeetCodeNet.G0001_0100.S0011_container_with_most_water { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers | ||
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1) | ||
// #2023_12_26_Time_248_ms_(11.15%)_Space_62.1_MB_(5.59%) | ||
|
||
public class Solution { | ||
public int MaxArea(int[] height) { | ||
int maxArea = -1; | ||
int left = 0; | ||
int right = height.Length - 1; | ||
|
||
while (left < right) { | ||
if (height[left] < height[right]) { | ||
maxArea = Math.Max(maxArea, height[left] * (right - left)); | ||
left++; | ||
} else { | ||
maxArea = Math.Max(maxArea, height[right] * (right - left)); | ||
right--; | ||
} | ||
} | ||
|
||
return maxArea; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
LeetCodeNet/G0001_0100/S0011_container_with_most_water/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
11\. Container With Most Water | ||
|
||
Medium | ||
|
||
Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> , where each represents a point at coordinate <code>(i, a<sub>i</sub>)</code>. `n` vertical lines are drawn such that the two endpoints of the line `i` is at <code>(i, a<sub>i</sub>)</code> and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. | ||
|
||
**Notice** that you may not slant the container. | ||
|
||
**Example 1:** | ||
|
||
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg) | ||
|
||
**Input:** height = [1,8,6,2,5,4,8,3,7] | ||
|
||
**Output:** 49 | ||
|
||
**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. | ||
|
||
**Example 2:** | ||
|
||
**Input:** height = [1,1] | ||
|
||
**Output:** 1 | ||
|
||
**Example 3:** | ||
|
||
**Input:** height = [4,3,2,1,4] | ||
|
||
**Output:** 16 | ||
|
||
**Example 4:** | ||
|
||
**Input:** height = [1,2,1] | ||
|
||
**Output:** 2 | ||
|
||
**Constraints:** | ||
|
||
* `n == height.length` | ||
* <code>2 <= n <= 10<sup>5</sup></code> | ||
* <code>0 <= height[i] <= 10<sup>4</sup></code> |
Oops, something went wrong.