Skip to content

Commit 11c867b

Browse files
authored
Added tasks 26-36
1 parent 53cf795 commit 11c867b

File tree

15 files changed

+523
-0
lines changed

15 files changed

+523
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0001_0100.S0026_remove_duplicates_from_sorted_array {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void RemoveDuplicates() {
8+
var solution = new Solution();
9+
var nums = new int[] { 1, 1, 2 };
10+
var expectedNums = new int[] { 1, 2 };
11+
var k = solution.RemoveDuplicates(nums);
12+
Assert.Equal(expectedNums.Length, k);
13+
for (int i = 0; i < k; i++) {
14+
Assert.Equal(expectedNums[i], nums[i]);
15+
}
16+
}
17+
18+
[Fact]
19+
public void RemoveDuplicates2() {
20+
var solution = new Solution();
21+
var nums = new int[] { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
22+
var expectedNums = new int[] { 0, 1, 2, 3, 4 };
23+
var k = solution.RemoveDuplicates(nums);
24+
Assert.Equal(expectedNums.Length, k);
25+
for (int i = 0; i < k; i++) {
26+
Assert.Equal(expectedNums[i], nums[i]);
27+
}
28+
}
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Linq;
2+
3+
namespace LeetCodeNet.G0001_0100.S0027_remove_element {
4+
5+
using Xunit;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void RemoveElement() {
10+
var solution = new Solution();
11+
var nums = new int[] { 3, 2, 2, 3 };
12+
var val = 3;
13+
var expectedNums = new int[] { 2, 2 };
14+
var k = solution.RemoveElement(nums, val);
15+
Assert.Equal(expectedNums.Length, k);
16+
var actualNums = nums.Take(k).ToArray();
17+
System.Array.Sort(actualNums);
18+
Assert.Equal(expectedNums, actualNums);
19+
}
20+
21+
[Fact]
22+
public void RemoveElement2() {
23+
var solution = new Solution();
24+
var nums = new int[] { 0, 1, 2, 2, 3, 0, 4, 2 };
25+
var val = 2;
26+
var expectedNums = new int[] { 0, 0, 1, 3, 4 };
27+
var k = solution.RemoveElement(nums, val);
28+
Assert.Equal(expectedNums.Length, k);
29+
var actualNums = nums.Take(k).ToArray();
30+
System.Array.Sort(actualNums);
31+
Assert.Equal(expectedNums, actualNums);
32+
}
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0001_0100.S0028_find_the_index_of_the_first_occurrence_in_a_string {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void StrStr() {
8+
Assert.Equal(0, new Solution().StrStr("sadbutsad", "sad"));
9+
}
10+
11+
[Fact]
12+
public void StrStr2() {
13+
Assert.Equal(-1, new Solution().StrStr("leetcode", "leeto"));
14+
}
15+
16+
[Fact]
17+
public void StrStr3() {
18+
Assert.Equal(0, new Solution().StrStr("", ""));
19+
}
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace LeetCodeNet.G0001_0100.S0030_substring_with_concatenation_of_all_words {
5+
public class SolutionTest {
6+
[Fact]
7+
public void FindSubstring() {
8+
var solution = new Solution();
9+
var result = solution.FindSubstring("barfoothefoobarman", new string[] { "foo", "bar" });
10+
var expected = new List<int> { 0, 9 };
11+
var resultList = new List<int>(result);
12+
resultList.Sort();
13+
expected.Sort();
14+
Assert.Equal(expected, resultList);
15+
}
16+
17+
[Fact]
18+
public void FindSubstring2() {
19+
var solution = new Solution();
20+
var result = solution.FindSubstring("wordgoodgoodgoodbestword", new string[] { "word", "good", "best", "word" });
21+
Assert.Equal(new List<int>(), result);
22+
}
23+
24+
[Fact]
25+
public void FindSubstring3() {
26+
var solution = new Solution();
27+
var result = solution.FindSubstring("barfoofoobarthefoobarman", new string[] { "bar", "foo", "the" });
28+
var expected = new List<int> { 6, 9, 12 };
29+
var resultList = new List<int>(result);
30+
resultList.Sort();
31+
expected.Sort();
32+
Assert.Equal(expected, resultList);
33+
}
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Xunit;
2+
3+
namespace LeetCodeNet.G0001_0100.S0036_valid_sudoku {
4+
public class SolutionTest {
5+
[Fact]
6+
public void IsValidSudoku_ValidBoard() {
7+
var solution = new Solution();
8+
var board = new char[][] {
9+
new char[] {'5','3','.','.','7','.','.','.','.'},
10+
new char[] {'6','.','.','1','9','5','.','.','.'},
11+
new char[] {'.','9','8','.','.','.','.','6','.'},
12+
new char[] {'8','.','.','.','6','.','.','.','3'},
13+
new char[] {'4','.','.','8','.','3','.','.','1'},
14+
new char[] {'7','.','.','.','2','.','.','.','6'},
15+
new char[] {'.','6','.','.','.','.','2','8','.'},
16+
new char[] {'.','.','.','4','1','9','.','.','5'},
17+
new char[] {'.','.','.','.','8','.','.','7','9'}
18+
};
19+
Assert.True(solution.IsValidSudoku(board));
20+
}
21+
22+
[Fact]
23+
public void IsValidSudoku_InvalidBoard() {
24+
var solution = new Solution();
25+
var board = new char[][] {
26+
new char[] {'8','3','.','.','7','.','.','.','.'},
27+
new char[] {'6','.','.','1','9','5','.','.','.'},
28+
new char[] {'.','9','8','.','.','.','.','6','.'},
29+
new char[] {'8','.','.','.','6','.','.','.','3'},
30+
new char[] {'4','.','.','8','.','3','.','.','1'},
31+
new char[] {'7','.','.','.','2','.','.','.','6'},
32+
new char[] {'.','6','.','.','.','.','2','8','.'},
33+
new char[] {'.','.','.','4','1','9','.','.','5'},
34+
new char[] {'.','.','.','.','8','.','.','7','9'}
35+
};
36+
Assert.False(solution.IsValidSudoku(board));
37+
}
38+
}
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0001_0100.S0026_remove_duplicates_from_sorted_array {
2+
3+
// #Easy #Top_Interview_Questions #Array #Two_Pointers #Udemy_Two_Pointers
4+
// #Top_Interview_150_Array/String #2025_06_30_Time_0_ms_(100.00%)_Space_50.94_MB_(81.18%)
5+
6+
public class Solution {
7+
public int RemoveDuplicates(int[] nums) {
8+
if (nums.Length == 0) {
9+
return 0;
10+
}
11+
int i = 0;
12+
for (int j = 1; j < nums.Length; j++) {
13+
if (nums[j] != nums[i]) {
14+
i++;
15+
nums[i] = nums[j];
16+
}
17+
}
18+
return i + 1;
19+
}
20+
}
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
26\. Remove Duplicates from Sorted Array
2+
3+
Easy
4+
5+
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
6+
7+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
8+
9+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10+
11+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
12+
13+
**Custom Judge:**
14+
15+
The judge will test your solution with the following code:
16+
17+
int[] nums = [...]; // Input array
18+
int[] expectedNums = [...]; // The expected answer with correct length
19+
20+
int k = removeDuplicates(nums); // Calls your implementation
21+
22+
assert k == expectedNums.length;
23+
for (int i = 0; i < k; i++) {
24+
assert nums[i] == expectedNums[i];
25+
}
26+
27+
If all assertions pass, then your solution will be **accepted**.
28+
29+
**Example 1:**
30+
31+
**Input:** nums = [1,1,2]
32+
33+
**Output:** 2, nums = [1,2,\_]
34+
35+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
36+
37+
**Example 2:**
38+
39+
**Input:** nums = [0,0,1,1,1,2,2,3,3,4]
40+
41+
**Output:** 5, nums = [0,1,2,3,4,\_,\_,\_,\_,\_]
42+
43+
**Explanation:** Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
44+
45+
**Constraints:**
46+
47+
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
48+
* `-100 <= nums[i] <= 100`
49+
* `nums` is sorted in **non-decreasing** order.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace LeetCodeNet.G0001_0100.S0027_remove_element {
2+
3+
// #Easy #Array #Two_Pointers #Top_Interview_150_Array/String
4+
// #2025_06_30_Time_0_ms_(100.00%)_Space_46.85_MB_(71.67%)
5+
6+
public class Solution {
7+
public int RemoveElement(int[] nums, int val) {
8+
int i = 0;
9+
for (int j = 0; j < nums.Length; j++) {
10+
if (nums[j] != val) {
11+
nums[i] = nums[j];
12+
i++;
13+
}
14+
}
15+
return i;
16+
}
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 27. Remove Element
2+
3+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` in-place. The order of the elements may be changed. Then return the number of elements in `nums` which are not equal to `val`.
4+
5+
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:
6+
7+
- Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
8+
- Return `k`.
9+
10+
## Example 1:
11+
12+
**Input:** nums = `[3,2,2,3]`, val = 3
13+
**Output:** 2, nums = `[2,2,_,_]`
14+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
15+
16+
## Example 2:
17+
18+
**Input:** nums = `[0,1,2,2,3,0,4,2]`, val = 2
19+
**Output:** 5, nums = `[0,1,4,0,3,_,_,_]`
20+
**Explanation:** Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
21+
22+
**Constraints:**
23+
24+
* `0 <= nums.length <= 100`
25+
* `0 <= nums[i] <= 50`
26+
* `0 <= val <= 100`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0001_0100.S0028_find_the_index_of_the_first_occurrence_in_a_string {
2+
3+
// #Easy #Top_Interview_Questions #String #Two_Pointers #String_Matching
4+
// #Programming_Skills_II_Day_1 #Top_Interview_150_Array/String
5+
// #2025_06_30_Time_0_ms_(100.00%)_Space_39.28_MB_(63.22%)
6+
7+
public class Solution {
8+
public int StrStr(string haystack, string needle) {
9+
if (string.IsNullOrEmpty(needle)) {
10+
return 0;
11+
}
12+
if (haystack.Length < needle.Length) {
13+
return -1;
14+
}
15+
for (int i = 0; i <= haystack.Length - needle.Length; i++) {
16+
if (haystack.Substring(i, needle.Length) == needle) {
17+
return i;
18+
}
19+
}
20+
return -1;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)