Skip to content

Commit

Permalink
Added tasks 3-10
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 22, 2023
1 parent 4b74a2a commit c35f8f3
Show file tree
Hide file tree
Showing 24 changed files with 852 additions and 0 deletions.
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"));
}
}
}
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}));
}
}
}
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"));
}
}
}
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 LeetCodeNet.Tests/G0001_0100/S0007_reverse_integer/SolutionTest.cs
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));
}
}
}
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"));
}
}
}
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));

Check warning on line 9 in LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[Fact]
public void IsPalindrome2() {
Assert.Equal(false, new Solution().IsPalindrome(-121));

Check warning on line 14 in LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[Fact]
public void IsPalindrome3() {
Assert.Equal(false, new Solution().IsPalindrome(10));

Check warning on line 19 in LeetCodeNet.Tests/G0001_0100/S0009_palindrome_number/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}
}
}
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

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)

Check warning on line 9 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[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

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)

Check warning on line 14 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[Fact]
public void IsMatch3() {
Assert.Equal(true, new Solution().IsMatch("ab", ".*"));

Check warning on line 19 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[Fact]
public void IsMatch4() {
Assert.Equal(true, new Solution().IsMatch("aab", "c*a*b"));

Check warning on line 24 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}

[Fact]
public void IsMatch5() {
Assert.Equal(false, new Solution().IsMatch("mississippi", "mis*is*p*."));

Check warning on line 29 in LeetCodeNet.Tests/G0001_0100/S0010_regular_expression_matching/SolutionTest.cs

View workflow job for this annotation

GitHub Actions / build-windows

Do not use Assert.Equal() to check for boolean conditions. (https://xunit.github.io/xunit.analyzers/rules/xUnit2004)
}
}
}
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;
}
}
}
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.
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 LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md
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>
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);
}
}
}
Loading

0 comments on commit c35f8f3

Please sign in to comment.