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
24 changed files
with
852 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...Net.Tests/G0001_0100/S0003_longest_substring_without_repeating_characters/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,22 @@ | ||
namespace LeetCodeNet.G0001_0100.S0003_longest_substring_without_repeating_characters { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void LengthOfLongestSubstring() { | ||
Assert.Equal(3, new Solution().LengthOfLongestSubstring("abcabcbb")); | ||
} | ||
|
||
[Fact] | ||
public void LengthOfLongestSubstring2() { | ||
Assert.Equal(1, new Solution().LengthOfLongestSubstring("bbbbb")); | ||
} | ||
|
||
[Fact] | ||
public void LengthOfLongestSubstring3() { | ||
Assert.Equal(3, new Solution().LengthOfLongestSubstring("pwwkew")); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
LeetCodeNet.Tests/G0001_0100/S0004_median_of_two_sorted_arrays/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,17 @@ | ||
namespace LeetCodeNet.G0001_0100.S0004_median_of_two_sorted_arrays { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void FindMedianSortedArrays() { | ||
Assert.Equal(2.0, new Solution().FindMedianSortedArrays(new int[] {1, 3}, new int[] {2})); | ||
} | ||
|
||
[Fact] | ||
public void FindMedianSortedArrays2() { | ||
Assert.Equal(2.5, new Solution().FindMedianSortedArrays(new int[] {1, 2}, new int[] {3, 4})); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
LeetCodeNet.Tests/G0001_0100/S0005_longest_palindromic_substring/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,17 @@ | ||
namespace LeetCodeNet.G0001_0100.S0005_longest_palindromic_substring { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void LongestPalindrome() { | ||
Assert.Equal("bab", new Solution().LongestPalindrome("babad")); | ||
} | ||
|
||
[Fact] | ||
public void LongestPalindrome2() { | ||
Assert.Equal("bb", new Solution().LongestPalindrome("cbbd")); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
LeetCodeNet.Tests/G0001_0100/S0006_zigzag_conversion/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,17 @@ | ||
namespace LeetCodeNet.G0001_0100.S0006_zigzag_conversion { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void Convert() { | ||
Assert.Equal("PAHNAPLSIIGYIR", new Solution().Convert("PAYPALISHIRING", 3)); | ||
} | ||
|
||
[Fact] | ||
public void Convert2() { | ||
Assert.Equal("PINALSIGYAHRPI", new Solution().Convert("PAYPALISHIRING", 4)); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/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,22 @@ | ||
namespace LeetCodeNet.G0001_0100.S0007_reverse_integer { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void Reverse() { | ||
Assert.Equal(321, new Solution().Reverse(123)); | ||
} | ||
|
||
[Fact] | ||
public void Reverse2() { | ||
Assert.Equal(-321, new Solution().Reverse(-123)); | ||
} | ||
|
||
[Fact] | ||
public void Reverse3() { | ||
Assert.Equal(21, new Solution().Reverse(120)); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
LeetCodeNet.Tests/G0001_0100/S0008_string_to_integer_atoi/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,32 @@ | ||
namespace LeetCodeNet.G0001_0100.S0008_string_to_integer_atoi { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MyAtoi() { | ||
Assert.Equal(42, new Solution().MyAtoi("42")); | ||
} | ||
|
||
[Fact] | ||
public void MyAtoi2() { | ||
Assert.Equal(-42, new Solution().MyAtoi(" -42")); | ||
} | ||
|
||
[Fact] | ||
public void MyAtoi3() { | ||
Assert.Equal(4193, new Solution().MyAtoi("4193 with words")); | ||
} | ||
|
||
[Fact] | ||
public void MyAtoi4() { | ||
Assert.Equal(0, new Solution().MyAtoi("words and 987")); | ||
} | ||
|
||
[Fact] | ||
public void MyAtoi5() { | ||
Assert.Equal(-2147483648, new Solution().MyAtoi("-91283472332")); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
LeetCodeNet.Tests/G0001_0100/S0009_palindrome_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,22 @@ | ||
namespace LeetCodeNet.G0001_0100.S0009_palindrome_number { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void IsPalindrome() { | ||
Assert.Equal(true, new Solution().IsPalindrome(121)); | ||
} | ||
|
||
[Fact] | ||
public void IsPalindrome2() { | ||
Assert.Equal(false, new Solution().IsPalindrome(-121)); | ||
} | ||
|
||
[Fact] | ||
public void IsPalindrome3() { | ||
Assert.Equal(false, new Solution().IsPalindrome(10)); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/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,32 @@ | ||
namespace LeetCodeNet.G0001_0100.S0010_regular_expression_matching { | ||
|
||
using Xunit; | ||
using System; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void IsMatch() { | ||
Assert.Equal(false, new Solution().IsMatch("aa", "a")); | ||
Check warning on line 9 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs GitHub Actions / build-windows
|
||
} | ||
|
||
[Fact] | ||
public void IsMatch2() { | ||
Assert.Equal(true, new Solution().IsMatch("aa", "a*")); | ||
Check warning on line 14 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs GitHub Actions / build-windows
|
||
} | ||
|
||
[Fact] | ||
public void IsMatch3() { | ||
Assert.Equal(true, new Solution().IsMatch("ab", ".*")); | ||
} | ||
|
||
[Fact] | ||
public void IsMatch4() { | ||
Assert.Equal(true, new Solution().IsMatch("aab", "c*a*b")); | ||
} | ||
|
||
[Fact] | ||
public void IsMatch5() { | ||
Assert.Equal(false, new Solution().IsMatch("mississippi", "mis*is*p*.")); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/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,34 @@ | ||
namespace LeetCodeNet.G0001_0100.S0003_longest_substring_without_repeating_characters { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window | ||
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings | ||
// #Big_O_Time_O(n)_Space_O(1) #2023_12_22_Time_50_ms_(98.40%)_Space_41.9_MB_(30.26%) | ||
|
||
public class Solution { | ||
public int LengthOfLongestSubstring(string s) { | ||
int[] lastIndices = new int[256]; | ||
for (int i = 0; i < 256; i++) { | ||
lastIndices[i] = -1; | ||
} | ||
int maxLen = 0; | ||
int curLen = 0; | ||
int start = 0; | ||
for (int i = 0; i < s.Length; i++) { | ||
char cur = s[i]; | ||
if (lastIndices[cur] < start) { | ||
lastIndices[cur] = i; | ||
curLen++; | ||
} else { | ||
int lastIndex = lastIndices[cur]; | ||
start = lastIndex + 1; | ||
curLen = i - start + 1; | ||
lastIndices[cur] = i; | ||
} | ||
if (curLen > maxLen) { | ||
maxLen = curLen; | ||
} | ||
} | ||
return maxLen; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...deNet/G0001_0100/S0003_longest_substring_without_repeating_characters/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,40 @@ | ||
3\. Longest Substring Without Repeating Characters | ||
|
||
Medium | ||
|
||
Given a string `s`, find the length of the **longest substring** without repeating characters. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "abcabcbb" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The answer is "abc", with the length of 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "bbbbb" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The answer is "b", with the length of 1. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "pwwkew" | ||
|
||
**Output:** 3 | ||
|
||
**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. | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "" | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code> | ||
* `s` consists of English letters, digits, symbols and spaces. |
38 changes: 38 additions & 0 deletions
38
LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/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,38 @@ | ||
namespace LeetCodeNet.G0001_0100.S0004_median_of_two_sorted_arrays { | ||
|
||
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer | ||
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2023_12_22_Time_83_ms_(96.35%)_Space_54_MB_(6.56%) | ||
|
||
public class Solution { | ||
public double FindMedianSortedArrays(int[] nums1, int[] nums2) { | ||
if (nums2.Length < nums1.Length) { | ||
return FindMedianSortedArrays(nums2, nums1); | ||
} | ||
int cut1; | ||
int cut2; | ||
int n1 = nums1.Length; | ||
int n2 = nums2.Length; | ||
int low = 0; | ||
int high = n1; | ||
while (low <= high) { | ||
cut1 = (low + high) / 2; | ||
cut2 = ((n1 + n2 + 1) / 2) - cut1; | ||
int l1 = cut1 == 0 ? int.MinValue : nums1[cut1 - 1]; | ||
int l2 = cut2 == 0 ? int.MinValue : nums2[cut2 - 1]; | ||
int r1 = cut1 == n1 ? int.MaxValue : nums1[cut1]; | ||
int r2 = cut2 == n2 ? int.MaxValue : nums2[cut2]; | ||
if (l1 <= r2 && l2 <= r1) { | ||
if ((n1 + n2) % 2 == 0) { | ||
return (Math.Max(l1, l2) + Math.Min(r1, r2)) / 2.0; | ||
} | ||
return Math.Max(l1, l2); | ||
} else if (l1 > r2) { | ||
high = cut1 - 1; | ||
} else { | ||
low = cut1 + 1; | ||
} | ||
} | ||
return 0.0; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/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,50 @@ | ||
4\. Median of Two Sorted Arrays | ||
|
||
Hard | ||
|
||
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays. | ||
|
||
The overall run time complexity should be `O(log (m+n))`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums1 = [1,3], nums2 = [2] | ||
|
||
**Output:** 2.00000 | ||
|
||
**Explanation:** merged array = [1,2,3] and median is 2. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums1 = [1,2], nums2 = [3,4] | ||
|
||
**Output:** 2.50000 | ||
|
||
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums1 = [0,0], nums2 = [0,0] | ||
|
||
**Output:** 0.00000 | ||
|
||
**Example 4:** | ||
|
||
**Input:** nums1 = [], nums2 = [1] | ||
|
||
**Output:** 1.00000 | ||
|
||
**Example 5:** | ||
|
||
**Input:** nums1 = [2], nums2 = [] | ||
|
||
**Output:** 2.00000 | ||
|
||
**Constraints:** | ||
|
||
* `nums1.length == m` | ||
* `nums2.length == n` | ||
* `0 <= m <= 1000` | ||
* `0 <= n <= 1000` | ||
* `1 <= m + n <= 2000` | ||
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code> |
38 changes: 38 additions & 0 deletions
38
LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/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,38 @@ | ||
namespace LeetCodeNet.G0001_0100.S0005_longest_palindromic_substring { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming | ||
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming | ||
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n) | ||
// #2023_12_22_Time_61_ms_(99.50%)_Space_40_MB_(56.86%) | ||
|
||
public class Solution { | ||
public string LongestPalindrome(string s) { | ||
if (s.Length == 1) return s; | ||
int res = 0; | ||
int l = 0; | ||
int r = 0; | ||
int len = s.Length; | ||
for (int i = 0; i < len; i ++) { | ||
for (int diff = 1; i - diff + 1 >= 0 && i + diff < len; diff ++) { | ||
if (s[i - diff + 1] != s[i + diff]) break; | ||
else if (res < diff * 2) { | ||
res = diff * 2; | ||
l = i - diff + 1; | ||
r = i + diff; | ||
} | ||
} | ||
} | ||
for (int i = 0; i < len; i ++) { | ||
for (int diff = 1; i - diff >= 0 && i + diff < len; diff ++) { | ||
if (s[i - diff] != s[i + diff]) break; | ||
else if (res < diff * 2 + 1) { | ||
res = diff * 2 + 1; | ||
l = i - diff; | ||
r = i + diff; | ||
} | ||
} | ||
} | ||
return s.Substring(l, r - l + 1); | ||
} | ||
} | ||
} |
Oops, something went wrong.