Skip to content

Commit 36af8e5

Browse files
authored
Added tasks 69-82
1 parent 06857d9 commit 36af8e5

File tree

15 files changed

+504
-0
lines changed

15 files changed

+504
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LeetCodeNet.G0001_0100.S0069_sqrtx {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MySqrt() {
8+
var solution = new Solution();
9+
Assert.Equal(2, solution.MySqrt(4));
10+
}
11+
12+
[Fact]
13+
public void MySqrt2() {
14+
var solution = new Solution();
15+
Assert.Equal(2, solution.MySqrt(8));
16+
}
17+
18+
[Fact]
19+
public void MySqrt3() {
20+
var solution = new Solution();
21+
Assert.Equal(0, solution.MySqrt(0));
22+
}
23+
24+
[Fact]
25+
public void MySqrt4() {
26+
var solution = new Solution();
27+
Assert.Equal(1, solution.MySqrt(1));
28+
}
29+
30+
[Fact]
31+
public void MySqrt5() {
32+
var solution = new Solution();
33+
Assert.Equal(46340, solution.MySqrt(2147395600));
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LeetCodeNet.G0001_0100.S0071_simplify_path {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void SimplifyPath() {
8+
var solution = new Solution();
9+
Assert.Equal("/home", solution.SimplifyPath("/home/"));
10+
}
11+
12+
[Fact]
13+
public void SimplifyPath2() {
14+
var solution = new Solution();
15+
Assert.Equal("/", solution.SimplifyPath("/../"));
16+
}
17+
18+
[Fact]
19+
public void SimplifyPath3() {
20+
var solution = new Solution();
21+
Assert.Equal("/home/foo", solution.SimplifyPath("/home//foo/"));
22+
}
23+
24+
[Fact]
25+
public void SimplifyPath4() {
26+
var solution = new Solution();
27+
Assert.Equal("/c", solution.SimplifyPath("/a/./b/../../c/"));
28+
}
29+
30+
[Fact]
31+
public void SimplifyPath5() {
32+
var solution = new Solution();
33+
Assert.Equal("/a/b/c", solution.SimplifyPath("/a//b////c/d//././/.."));
34+
}
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0001_0100.S0077_combinations {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
public class SolutionTest {
8+
private List<List<int>> SortCombinations(IList<IList<int>> combos) {
9+
return combos.Select(x => x.OrderBy(i => i).ToList()).OrderBy(x => string.Join(",", x)).ToList();
10+
}
11+
12+
[Fact]
13+
public void Combine() {
14+
var solution = new Solution();
15+
IList<IList<int>> expected = new List<IList<int>> {
16+
new List<int> {2,4}, new List<int> {3,4}, new List<int> {2,3}, new List<int> {1,2}, new List<int> {1,3}, new List<int> {1,4}
17+
};
18+
var result = solution.Combine(4, 2);
19+
Assert.Equal(SortCombinations(expected), SortCombinations(result));
20+
}
21+
22+
[Fact]
23+
public void Combine2() {
24+
var solution = new Solution();
25+
var expected = new List<IList<int>> {
26+
new List<int> {1}
27+
};
28+
var result = solution.Combine(1, 1);
29+
Assert.Equal(SortCombinations(expected), SortCombinations(result));
30+
}
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace LeetCodeNet.G0001_0100.S0080_remove_duplicates_from_sorted_array_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void RemoveDuplicates() {
8+
var solution = new Solution();
9+
int[] nums = {1,1,1,2,2,3};
10+
int k = solution.RemoveDuplicates(nums);
11+
Assert.Equal(5, k);
12+
Assert.Equal(new int[] {1,1,2,2,3}, nums[..k]);
13+
}
14+
15+
[Fact]
16+
public void RemoveDuplicates2() {
17+
var solution = new Solution();
18+
int[] nums = {0,0,1,1,1,1,2,3,3};
19+
int k = solution.RemoveDuplicates(nums);
20+
Assert.Equal(7, k);
21+
Assert.Equal(new int[] {0,0,1,1,2,3,3}, nums[..k]);
22+
}
23+
24+
[Fact]
25+
public void RemoveDuplicates3() {
26+
var solution = new Solution();
27+
int[] nums = {1,2,3,4};
28+
int k = solution.RemoveDuplicates(nums);
29+
Assert.Equal(4, k);
30+
Assert.Equal(new int[] {1,2,3,4}, nums[..k]);
31+
}
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace LeetCodeNet.G0001_0100.S0082_remove_duplicates_from_sorted_list_ii {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
private ListNode BuildList(int[] vals) {
8+
ListNode dummy = new ListNode(0);
9+
ListNode curr = dummy;
10+
foreach (var v in vals) {
11+
curr.next = new ListNode(v);
12+
curr = curr.next;
13+
}
14+
return dummy.next;
15+
}
16+
17+
private int[] ToArray(ListNode head) {
18+
var list = new System.Collections.Generic.List<int>();
19+
while (head != null) {
20+
list.Add(head.val);
21+
head = head.next;
22+
}
23+
return list.ToArray();
24+
}
25+
26+
[Fact]
27+
public void DeleteDuplicates() {
28+
var solution = new Solution();
29+
var head = BuildList(new int[] {1,2,3,3,4,4,5});
30+
var result = solution.DeleteDuplicates(head);
31+
Assert.Equal(new int[] {1,2,5}, ToArray(result));
32+
}
33+
34+
[Fact]
35+
public void DeleteDuplicates2() {
36+
var solution = new Solution();
37+
var head = BuildList(new int[] {1,1,1,2,3});
38+
var result = solution.DeleteDuplicates(head);
39+
Assert.Equal(new int[] {2,3}, ToArray(result));
40+
}
41+
42+
[Fact]
43+
public void DeleteDuplicates3() {
44+
var solution = new Solution();
45+
var head = BuildList(new int[] {1,2,3});
46+
var result = solution.DeleteDuplicates(head);
47+
Assert.Equal(new int[] {1,2,3}, ToArray(result));
48+
}
49+
}
50+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LeetCodeNet.G0001_0100.S0069_sqrtx {
2+
3+
// #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4
4+
// #Top_Interview_150_Math #2025_07_04_Time_0_ms_(100.00%)_Space_29.30_MB_(44.18%)
5+
6+
public class Solution {
7+
public int MySqrt(int x) {
8+
if (x < 2) {
9+
return x;
10+
}
11+
int left = 1;
12+
int right = x / 2;
13+
int ans = 0;
14+
while (left <= right) {
15+
int mid = left + (right - left) / 2;
16+
if ((long)mid * mid == x) {
17+
return mid;
18+
}
19+
if ((long)mid * mid < x) {
20+
ans = mid;
21+
left = mid + 1;
22+
} else {
23+
right = mid - 1;
24+
}
25+
}
26+
return ans;
27+
}
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
69\. Sqrt(x)
2+
3+
Easy
4+
5+
Given a non-negative integer `x`, compute and return _the square root of_ `x`.
6+
7+
Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.
8+
9+
**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or `x ** 0.5`.
10+
11+
**Example 1:**
12+
13+
**Input:** x = 4
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** x = 8
20+
21+
**Output:** 2
22+
23+
**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
24+
25+
**Constraints:**
26+
27+
* <code>0 <= x <= 2<sup>31</sup> - 1</code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LeetCodeNet.G0001_0100.S0071_simplify_path {
2+
3+
// #Medium #String #Stack #Top_Interview_150_Stack
4+
// #2025_07_04_Time_2_ms_(90.87%)_Space_42.38_MB_(89.63%)
5+
6+
public class Solution {
7+
public string SimplifyPath(string path) {
8+
var parts = path.Split('/');
9+
var newParts = new List<string>();
10+
for (var i = 0; i < parts.Length; i++) {
11+
if (parts[i] == string.Empty) {
12+
continue;
13+
}
14+
if (parts[i] == ".") {
15+
continue;
16+
}
17+
if (parts[i] == "..") {
18+
if (newParts.Count > 0)
19+
newParts.RemoveAt(newParts.Count - 1);
20+
continue;
21+
}
22+
newParts.Add(parts[i]);
23+
}
24+
return $"/{string.Join("/", newParts)}";
25+
}
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
71\. Simplify Path
2+
3+
Medium
4+
5+
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**.
6+
7+
In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level, and any multiple consecutive slashes (i.e. `'//'`) are treated as a single slash `'/'`. For this problem, any other format of periods such as `'...'` are treated as file/directory names.
8+
9+
The **canonical path** should have the following format:
10+
11+
* The path starts with a single slash `'/'`.
12+
* Any two directories are separated by a single slash `'/'`.
13+
* The path does not end with a trailing `'/'`.
14+
* The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period `'.'` or double period `'..'`)
15+
16+
Return _the simplified **canonical path**_.
17+
18+
**Example 1:**
19+
20+
**Input:** path = "/home/"
21+
22+
**Output:** "/home"
23+
24+
**Explanation:** Note that there is no trailing slash after the last directory name.
25+
26+
**Example 2:**
27+
28+
**Input:** path = "/../"
29+
30+
**Output:** "/"
31+
32+
**Explanation:** Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
33+
34+
**Example 3:**
35+
36+
**Input:** path = "/home//foo/"
37+
38+
**Output:** "/home/foo"
39+
40+
**Explanation:** In the canonical path, multiple consecutive slashes are replaced by a single one.
41+
42+
**Example 4:**
43+
44+
**Input:** path = "/a/./b/../../c/"
45+
46+
**Output:** "/c"
47+
48+
**Constraints:**
49+
50+
* `1 <= path.length <= 3000`
51+
* `path` consists of English letters, digits, period `'.'`, slash `'/'` or `'_'`.
52+
* `path` is a valid absolute Unix path.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LeetCodeNet.G0001_0100.S0077_combinations {
2+
3+
// #Medium #Backtracking #Algorithm_I_Day_11_Recursion_Backtracking #Top_Interview_150_Backtracking
4+
// #2025_07_04_Time_29_ms_(89.17%)_Space_97.46_MB_(59.96%)
5+
6+
public class Solution {
7+
public IList<IList<int>> Combine(int n, int k) {
8+
var res = new List<IList<int>>();
9+
Backtrack(1, n, k, new List<int>(), res);
10+
return res;
11+
}
12+
private void Backtrack(int start, int n, int k, List<int> curr, List<IList<int>> res) {
13+
if (curr.Count == k) {
14+
res.Add(new List<int>(curr));
15+
return;
16+
}
17+
for (int i = start; i <= n; i++) {
18+
curr.Add(i);
19+
Backtrack(i + 1, n, k, curr, res);
20+
curr.RemoveAt(curr.Count - 1);
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)