From c35f8f312d9eafa00504708f7d45b87ca1448441 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 22 Dec 2023 11:11:37 +0200 Subject: [PATCH] Added tasks 3-10 --- .../SolutionTest.cs | 22 ++++ .../SolutionTest.cs | 17 +++ .../SolutionTest.cs | 17 +++ .../S0006_zigzag_conversion/SolutionTest.cs | 17 +++ .../S0007_reverse_integer/SolutionTest.cs | 22 ++++ .../SolutionTest.cs | 32 +++++ .../S0009_palindrome_number/SolutionTest.cs | 22 ++++ .../SolutionTest.cs | 32 +++++ .../Solution.cs | 34 ++++++ .../readme.md | 40 +++++++ .../Solution.cs | 38 ++++++ .../readme.md | 50 ++++++++ .../Solution.cs | 38 ++++++ .../readme.md | 34 ++++++ .../S0006_zigzag_conversion/Solution.cs | 37 ++++++ .../S0006_zigzag_conversion/readme.md | 39 ++++++ .../S0007_reverse_integer/Solution.cs | 19 +++ .../S0007_reverse_integer/readme.md | 35 ++++++ .../S0008_string_to_integer_atoi/Solution.cs | 44 +++++++ .../S0008_string_to_integer_atoi/readme.md | 113 ++++++++++++++++++ .../S0009_palindrome_number/Solution.cs | 20 ++++ .../S0009_palindrome_number/readme.md | 41 +++++++ .../Solution.cs | 33 +++++ .../readme.md | 56 +++++++++ 24 files changed, 852 insertions(+) create mode 100644 LeetCodeNet.Tests/G0001_0100/S0003_longest_substring_without_repeating_characters/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0004_median_of_two_sorted_arrays/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0005_longest_palindromic_substring/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0006_zigzag_conversion/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0008_string_to_integer_atoi/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs create mode 100644 LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs create mode 100644 LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0006_zigzag_conversion/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0006_zigzag_conversion/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0007_reverse_integer/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0007_reverse_integer/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0009_palindrome_number/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0009_palindrome_number/readme.md create mode 100644 LeetCodeNet/G0001_0100/S0010_regular_expression_matching/Solution.cs create mode 100644 LeetCodeNet/G0001_0100/S0010_regular_expression_matching/readme.md diff --git a/LeetCodeNet.Tests/G0001_0100/S0003_longest_substring_without_repeating_characters/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0003_longest_substring_without_repeating_characters/SolutionTest.cs new file mode 100644 index 0000000..7247d6c --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0003_longest_substring_without_repeating_characters/SolutionTest.cs @@ -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")); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0004_median_of_two_sorted_arrays/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0004_median_of_two_sorted_arrays/SolutionTest.cs new file mode 100644 index 0000000..3eab76d --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0004_median_of_two_sorted_arrays/SolutionTest.cs @@ -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})); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0005_longest_palindromic_substring/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0005_longest_palindromic_substring/SolutionTest.cs new file mode 100644 index 0000000..9fcf6a0 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0005_longest_palindromic_substring/SolutionTest.cs @@ -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")); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0006_zigzag_conversion/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0006_zigzag_conversion/SolutionTest.cs new file mode 100644 index 0000000..e962cb8 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0006_zigzag_conversion/SolutionTest.cs @@ -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)); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/SolutionTest.cs new file mode 100644 index 0000000..46be561 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/SolutionTest.cs @@ -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)); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0008_string_to_integer_atoi/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0008_string_to_integer_atoi/SolutionTest.cs new file mode 100644 index 0000000..1682aa9 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0008_string_to_integer_atoi/SolutionTest.cs @@ -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")); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs new file mode 100644 index 0000000..162ebe4 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs @@ -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)); + } +} +} diff --git a/LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs b/LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs new file mode 100644 index 0000000..415adf0 --- /dev/null +++ b/LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs @@ -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")); + } + + [Fact] + public void IsMatch2() { + Assert.Equal(true, new Solution().IsMatch("aa", "a*")); + } + + [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*.")); + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/Solution.cs b/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/Solution.cs new file mode 100644 index 0000000..dda6154 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/Solution.cs @@ -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; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/readme.md b/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/readme.md new file mode 100644 index 0000000..bf1ef46 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0003_longest_substring_without_repeating_characters/readme.md @@ -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:** + +* 0 <= s.length <= 5 * 104 +* `s` consists of English letters, digits, symbols and spaces. \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/Solution.cs b/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/Solution.cs new file mode 100644 index 0000000..1e89c21 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/Solution.cs @@ -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; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md b/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md new file mode 100644 index 0000000..a463374 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md @@ -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` +* -106 <= nums1[i], nums2[i] <= 106 \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/Solution.cs b/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/Solution.cs new file mode 100644 index 0000000..5b47a40 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/Solution.cs @@ -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); + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/readme.md b/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/readme.md new file mode 100644 index 0000000..883ff5c --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0005_longest_palindromic_substring/readme.md @@ -0,0 +1,34 @@ +5\. Longest Palindromic Substring + +Medium + +Given a string `s`, return _the longest palindromic substring_ in `s`. + +**Example 1:** + +**Input:** s = "babad" + +**Output:** "bab" **Note:** "aba" is also a valid answer. + +**Example 2:** + +**Input:** s = "cbbd" + +**Output:** "bb" + +**Example 3:** + +**Input:** s = "a" + +**Output:** "a" + +**Example 4:** + +**Input:** s = "ac" + +**Output:** "a" + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consist of only digits and English letters. \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/Solution.cs b/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/Solution.cs new file mode 100644 index 0000000..9ef5f99 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/Solution.cs @@ -0,0 +1,37 @@ +namespace LeetCodeNet.G0001_0100.S0006_zigzag_conversion { + +// #Medium #String #2023_12_22_Time_59_ms_(99.87%)_Space_45.1_MB_(74.68%) + +using System.Text; + +public class Solution { + public string Convert(string s, int numRows) { + int sLen = s.Length; + if (numRows == 1) { + return s; + } + int maxDist = numRows * 2 - 2; + StringBuilder buf = new StringBuilder(); + for (int i = 0; i < numRows; i++) { + int index = i; + if (i == 0 || i == numRows - 1) { + while (index < sLen) { + buf.Append(s[index]); + index += maxDist; + } + } else { + while (index < sLen) { + buf.Append(s[index]); + index += maxDist - i * 2; + if (index >= sLen) { + break; + } + buf.Append(s[index]); + index += i * 2; + } + } + } + return buf.ToString(); + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/readme.md b/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/readme.md new file mode 100644 index 0000000..4f55d57 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0006_zigzag_conversion/readme.md @@ -0,0 +1,39 @@ +6\. Zigzag Conversion + +Medium + +The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) + +P A H N A P L S I I G Y I R + +And then read line by line: `"PAHNAPLSIIGYIR"` + +Write the code that will take a string and make this conversion given a number of rows: + +string convert(string s, int numRows); + +**Example 1:** + +**Input:** s = "PAYPALISHIRING", numRows = 3 + +**Output:** "PAHNAPLSIIGYIR" + +**Example 2:** + +**Input:** s = "PAYPALISHIRING", numRows = 4 + +**Output:** "PINALSIGYAHRPI" + +**Explanation:** P I N A L S I G Y A H R P I + +**Example 3:** + +**Input:** s = "A", numRows = 1 + +**Output:** "A" + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`. +* `1 <= numRows <= 1000` \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0007_reverse_integer/Solution.cs b/LeetCodeNet/G0001_0100/S0007_reverse_integer/Solution.cs new file mode 100644 index 0000000..9cf2ba0 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0007_reverse_integer/Solution.cs @@ -0,0 +1,19 @@ +namespace LeetCodeNet.G0001_0100.S0007_reverse_integer { + +// #Medium #Top_Interview_Questions #Math #Udemy_Integers +// #2023_12_22_Time_23_ms_(59.02%)_Space_26.3_MB_(99.19%) + +public class Solution { + public int Reverse(int x) { + long rev = 0; + while (x != 0) { + rev = (rev * 10) + (x % 10); + x /= 10; + } + if (rev > int.MaxValue || rev < int.MinValue) { + return 0; + } + return (int) rev; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0007_reverse_integer/readme.md b/LeetCodeNet/G0001_0100/S0007_reverse_integer/readme.md new file mode 100644 index 0000000..81dbb62 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0007_reverse_integer/readme.md @@ -0,0 +1,35 @@ +7\. Reverse Integer + +Medium + +Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If reversing `x` causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return `0`. + +**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).** + +**Example 1:** + +**Input:** x = 123 + +**Output:** 321 + +**Example 2:** + +**Input:** x = -123 + +**Output:** -321 + +**Example 3:** + +**Input:** x = 120 + +**Output:** 21 + +**Example 4:** + +**Input:** x = 0 + +**Output:** 0 + +**Constraints:** + +* -231 <= x <= 231 - 1 \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/Solution.cs b/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/Solution.cs new file mode 100644 index 0000000..33aea84 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/Solution.cs @@ -0,0 +1,44 @@ +namespace LeetCodeNet.G0001_0100.S0008_string_to_integer_atoi { + +// #Medium #Top_Interview_Questions #String #2023_12_22_Time_43_ms_(99.83%)_Space_39.7_MB_(23.14%) + +public class Solution { + public int MyAtoi(string str) { + if (str == null || str.Length == 0) { + return 0; + } + int i = 0; + bool negetiveSign = false; + char[] input = str.ToCharArray(); + while (i < input.Length && input[i] == ' ') { + i++; + } + if (i == input.Length) { + return 0; + } else if (input[i] == '+') { + i++; + } else if (input[i] == '-') { + i++; + negetiveSign = true; + } + int num = 0; + while (i < input.Length && input[i] <= '9' && input[i] >= '0') { + // current char + int tem = input[i] - '0'; + tem = negetiveSign ? -tem : tem; + // avoid invalid number like 038 + if (num == 0 && tem == '0') { + i++; + } else if (num == int.MinValue / 10 && tem <= -8 || num < int.MinValue / 10) { + return int.MinValue; + } else if (num == int.MaxValue / 10 && tem >= 7 || num > int.MaxValue / 10) { + return int.MaxValue; + } else { + num = num * 10 + tem; + i++; + } + } + return num; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/readme.md b/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/readme.md new file mode 100644 index 0000000..af67f64 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0008_string_to_integer_atoi/readme.md @@ -0,0 +1,113 @@ +8\. String to Integer (atoi) + +Medium + +Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function). + +The algorithm for `myAtoi(string s)` is as follows: + +1. Read in and ignore any leading whitespace. +2. Check if the next character (if not already at the end of the string) is `'-'` or `'+'`. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. +3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. +4. Convert these digits into an integer (i.e. `"123" -> 123`, `"0032" -> 32`). If no digits were read, then the integer is `0`. Change the sign as necessary (from step 2). +5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1. +6. Return the integer as the final result. + +**Note:** + +* Only the space character `' '` is considered a whitespace character. +* **Do not ignore** any characters other than the leading whitespace or the rest of the string after the digits. + +**Example 1:** + +**Input:** s = "42" + +**Output:** 42 + +**Explanation:** The underlined characters are what is read in, the caret is the current reader position. + + Step 1: "42" (no characters read because there is no leading whitespace) + ^ + Step 2: "42" (no characters read because there is neither a '-' nor '+') + ^ + Step 3: "42" ("42" is read in) + ^ + +The parsed integer is 42. Since 42 is in the range [-231, 231 - 1], the final result is 42. + +**Example 2:** + +**Input:** s = " -42" + +**Output:** -42 + +**Explanation:** + + Step 1: " -42" (leading whitespace is read and ignored) + ^ + Step 2: " -42" ('-' is read, so the result should be negative) + ^ + Step 3: " -42" ("42" is read in) + ^ + The parsed integer is -42. + +Since -42 is in the range [-231, 231 - 1], the final result is -42. + +**Example 3:** + +**Input:** s = "4193 with words" + +**Output:** 4193 + +**Explanation:** + + Step 1: "4193 with words" (no characters read because there is no leading whitespace) + ^ + Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') + ^ + Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit) + ^ + The parsed integer is 4193. + +Since 4193 is in the range [-231, 231 - 1], the final result is 4193. + +**Example 4:** + +**Input:** s = "words and 987" + +**Output:** 0 + +**Explanation:** + + Step 1: "words and 987" (no characters read because there is no leading whitespace) + ^ + Step 2: "words and 987" (no characters read because there is neither a '-' nor '+') + ^ + Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w') + ^ + The parsed integer is 0 because no digits were read. + +Since 0 is in the range [-231, 231 - 1], the final result is 0. + +**Example 5:** + +**Input:** s = "-91283472332" + +**Output:** -2147483648 + +**Explanation:** + + Step 1: "-91283472332" (no characters read because there is no leading whitespace) + ^ + Step 2: "-91283472332" ('-' is read, so the result should be negative) + ^ + Step 3: "-91283472332" ("91283472332" is read in) + ^ + The parsed integer is -91283472332. + +Since -91283472332 is less than the lower bound of the range [-231, 231 - 1], the final result is clamped to -231 = -2147483648. + +**Constraints:** + +* `0 <= s.length <= 200` +* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`. \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0009_palindrome_number/Solution.cs b/LeetCodeNet/G0001_0100/S0009_palindrome_number/Solution.cs new file mode 100644 index 0000000..a515ae4 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0009_palindrome_number/Solution.cs @@ -0,0 +1,20 @@ +namespace LeetCodeNet.G0001_0100.S0009_palindrome_number { + +// #Easy #Math #Udemy_Integers #2023_12_22_Time_42_ms_(34.61%)_Space_31_MB_(36.04%) + +public class Solution { + public bool IsPalindrome(int x) { + if (x < 0) { + return false; + } + int rev = 0; + int localX = x; + while (localX > 0) { + rev *= 10; + rev += localX % 10; + localX /= 10; + } + return rev == x; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0009_palindrome_number/readme.md b/LeetCodeNet/G0001_0100/S0009_palindrome_number/readme.md new file mode 100644 index 0000000..f05939b --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0009_palindrome_number/readme.md @@ -0,0 +1,41 @@ +9\. Palindrome Number + +Easy + +Given an integer `x`, return `true` if `x` is palindrome integer. + +An integer is a **palindrome** when it reads the same backward as forward. For example, `121` is palindrome while `123` is not. + +**Example 1:** + +**Input:** x = 121 + +**Output:** true + +**Example 2:** + +**Input:** x = -121 + +**Output:** false + +**Explanation:** From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. + +**Example 3:** + +**Input:** x = 10 + +**Output:** false + +**Explanation:** Reads 01 from right to left. Therefore it is not a palindrome. + +**Example 4:** + +**Input:** x = -101 + +**Output:** false + +**Constraints:** + +* -231 <= x <= 231 - 1 + +**Follow up:** Could you solve it without converting the integer to a string? \ No newline at end of file diff --git a/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/Solution.cs b/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/Solution.cs new file mode 100644 index 0000000..9e197dd --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/Solution.cs @@ -0,0 +1,33 @@ +namespace LeetCodeNet.G0001_0100.S0010_regular_expression_matching { + +// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion +// #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n) +// #2023_12_22_Time_59_ms_(96.10%)_Space_40.5_MB_(47.63%) + +public class Solution { + private bool?[,] cache; + + public bool IsMatch(string s, string p) { + cache = new bool?[s.Length + 1, p.Length + 1]; + return IsMatch(s, p, 0, 0); + } + + private bool IsMatch(string s, string p, int i, int j) { + if (j == p.Length) { + return i == s.Length; + } + bool result; + if (cache[i, j] != null) { + return cache[i, j].Value; + } + bool firstMatch = i < s.Length && (s[i] == p[j] || p[j] == '.'); + if ((j + 1) < p.Length && p[j + 1] == '*') { + result = (firstMatch && IsMatch(s, p, i + 1, j)) || IsMatch(s, p, i, j + 2); + } else { + result = firstMatch && IsMatch(s, p, i + 1, j + 1); + } + cache[i, j] = result; + return result; + } +} +} diff --git a/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/readme.md b/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/readme.md new file mode 100644 index 0000000..f614e74 --- /dev/null +++ b/LeetCodeNet/G0001_0100/S0010_regular_expression_matching/readme.md @@ -0,0 +1,56 @@ +10\. Regular Expression Matching + +Hard + +Given an input string `s` and a pattern `p`, implement regular expression matching with support for `'.'` and `'*'` where: + +* `'.'` Matches any single character. +* `'*'` Matches zero or more of the preceding element. + +The matching should cover the **entire** input string (not partial). + +**Example 1:** + +**Input:** s = "aa", p = "a" + +**Output:** false + +**Explanation:** "a" does not match the entire string "aa". + +**Example 2:** + +**Input:** s = "aa", p = "a\*" + +**Output:** true + +**Explanation:** '\*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". + +**Example 3:** + +**Input:** s = "ab", p = ".\*" + +**Output:** true + +**Explanation:** ".\*" means "zero or more (\*) of any character (.)". + +**Example 4:** + +**Input:** s = "aab", p = "c\*a\*b" + +**Output:** true + +**Explanation:** c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab". + +**Example 5:** + +**Input:** s = "mississippi", p = "mis\*is\*p\*." + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 20` +* `1 <= p.length <= 30` +* `s` contains only lowercase English letters. +* `p` contains only lowercase English letters, `'.'`, and `'*'`. +* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match. \ No newline at end of file