Skip to content

Commit f3668c6

Browse files
authored
Added tasks 50-58
1 parent 11c867b commit f3668c6

File tree

15 files changed

+457
-0
lines changed

15 files changed

+457
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Xunit;
2+
3+
namespace LeetCodeNet.G0001_0100.S0050_powx_n {
4+
public class SolutionTest {
5+
[Fact]
6+
public void MyPow_PositiveExponent() {
7+
Assert.True(System.Math.Abs(new Solution().MyPow(2.0, 10) - 1024.0) < 1e-5);
8+
}
9+
10+
[Fact]
11+
public void MyPow_DecimalBase() {
12+
Assert.True(System.Math.Abs(new Solution().MyPow(2.1, 3) - 9.261) < 1e-5);
13+
}
14+
15+
[Fact]
16+
public void MyPow_NegativeExponent() {
17+
Assert.True(System.Math.Abs(new Solution().MyPow(2.0, -2) - 0.25) < 1e-5);
18+
}
19+
20+
[Fact]
21+
public void MyPow_ZeroExponent() {
22+
Assert.True(System.Math.Abs(new Solution().MyPow(1.0, 0) - 1.0) < 1e-5);
23+
}
24+
25+
[Fact]
26+
public void MyPow_NegativeBase() {
27+
Assert.True(System.Math.Abs(new Solution().MyPow(-1.0, 1) - (-1.0)) < 1e-5);
28+
}
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Xunit;
2+
3+
namespace LeetCodeNet.G0001_0100.S0052_n_queens_ii {
4+
public class SolutionTest {
5+
[Fact]
6+
public void TotalNQueens_1() {
7+
Assert.Equal(1, new Solution().TotalNQueens(1));
8+
}
9+
10+
[Fact]
11+
public void TotalNQueens_4() {
12+
Assert.Equal(2, new Solution().TotalNQueens(4));
13+
}
14+
15+
[Fact]
16+
public void TotalNQueens_5() {
17+
Assert.Equal(10, new Solution().TotalNQueens(5));
18+
}
19+
20+
[Fact]
21+
public void TotalNQueens_8() {
22+
Assert.Equal(92, new Solution().TotalNQueens(8));
23+
}
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace LeetCodeNet.G0001_0100.S0054_spiral_matrix {
5+
public class SolutionTest {
6+
[Fact]
7+
public void SpiralOrder_3x3() {
8+
var solution = new Solution();
9+
int[][] matrix = new int[][] {
10+
new int[] {1, 2, 3},
11+
new int[] {4, 5, 6},
12+
new int[] {7, 8, 9}
13+
};
14+
var expected = new List<int> {1, 2, 3, 6, 9, 8, 7, 4, 5};
15+
Assert.Equal(expected, solution.SpiralOrder(matrix));
16+
}
17+
18+
[Fact]
19+
public void SpiralOrder_3x4() {
20+
var solution = new Solution();
21+
int[][] matrix = new int[][] {
22+
new int[] {1, 2, 3, 4},
23+
new int[] {5, 6, 7, 8},
24+
new int[] {9, 10, 11, 12}
25+
};
26+
var expected = new List<int> {1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7};
27+
Assert.Equal(expected, solution.SpiralOrder(matrix));
28+
}
29+
}
30+
}
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.S0057_insert_interval {
4+
public class SolutionTest {
5+
[Fact]
6+
public void Insert_Overlap() {
7+
var solution = new Solution();
8+
int[][] intervals = new int[][] {
9+
new int[] {1, 3},
10+
new int[] {6, 9}
11+
};
12+
int[] newInterval = new int[] {2, 5};
13+
int[][] expected = new int[][] {
14+
new int[] {1, 5},
15+
new int[] {6, 9}
16+
};
17+
Assert.Equal(expected, solution.Insert(intervals, newInterval));
18+
}
19+
20+
[Fact]
21+
public void Insert_MergeMultiple() {
22+
var solution = new Solution();
23+
int[][] intervals = new int[][] {
24+
new int[] {1, 2},
25+
new int[] {3, 5},
26+
new int[] {6, 7},
27+
new int[] {8, 10},
28+
new int[] {12, 16}
29+
};
30+
int[] newInterval = new int[] {4, 8};
31+
int[][] expected = new int[][] {
32+
new int[] {1, 2},
33+
new int[] {3, 10},
34+
new int[] {12, 16}
35+
};
36+
Assert.Equal(expected, solution.Insert(intervals, newInterval));
37+
}
38+
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Xunit;
2+
3+
namespace LeetCodeNet.G0001_0100.S0058_length_of_last_word {
4+
public class SolutionTest {
5+
[Fact]
6+
public void LengthOfLastWord_Normal() {
7+
Assert.Equal(5, new Solution().LengthOfLastWord("Hello World"));
8+
}
9+
10+
[Fact]
11+
public void LengthOfLastWord_Spaces() {
12+
Assert.Equal(4, new Solution().LengthOfLastWord(" fly me to the moon "));
13+
}
14+
15+
[Fact]
16+
public void LengthOfLastWord_MultipleWords() {
17+
Assert.Equal(6, new Solution().LengthOfLastWord("luffy is still joyboy"));
18+
}
19+
20+
[Fact]
21+
public void LengthOfLastWord_SingleChar() {
22+
Assert.Equal(1, new Solution().LengthOfLastWord("a"));
23+
}
24+
25+
[Fact]
26+
public void LengthOfLastWord_OnlySpaces() {
27+
Assert.Equal(0, new Solution().LengthOfLastWord(" "));
28+
}
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0001_0100.S0050_powx_n {
2+
3+
// #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math
4+
// #2025_07_01_Time_0_ms_(100.00%)_Space_29.14_MB_(83.57%)
5+
6+
public class Solution {
7+
public double MyPow(double x, int n) {
8+
if (n == 0) return 1.0;
9+
long N = n;
10+
if (N < 0) {
11+
x = 1 / x;
12+
N = -N;
13+
}
14+
double result = 1.0;
15+
while (N > 0) {
16+
if ((N & 1) == 1) result *= x;
17+
x *= x;
18+
N >>= 1;
19+
}
20+
return result;
21+
}
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
50\. Pow(x, n)
2+
3+
Medium
4+
5+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
6+
7+
**Example 1:**
8+
9+
**Input:** x = 2.00000, n = 10
10+
11+
**Output:** 1024.00000
12+
13+
**Example 2:**
14+
15+
**Input:** x = 2.10000, n = 3
16+
17+
**Output:** 9.26100
18+
19+
**Example 3:**
20+
21+
**Input:** x = 2.00000, n = -2
22+
23+
**Output:** 0.25000
24+
25+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
26+
27+
**Constraints:**
28+
29+
* `-100.0 < x < 100.0`
30+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
31+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace LeetCodeNet.G0001_0100.S0052_n_queens_ii {
2+
3+
// #Hard #Backtracking #Top_Interview_150_Backtracking
4+
// #2025_07_01_Time_1_ms_(95.31%)_Space_29.22_MB_(64.06%)
5+
6+
public class Solution {
7+
public int TotalNQueens(int n) {
8+
int count = 0;
9+
Solve(0, n, new bool[n], new bool[2 * n], new bool[2 * n], ref count);
10+
return count;
11+
}
12+
13+
private void Solve(int row, int n, bool[] cols, bool[] d1, bool[] d2, ref int count) {
14+
if (row == n) {
15+
count++;
16+
return;
17+
}
18+
for (int col = 0; col < n; col++) {
19+
int id1 = col - row + n;
20+
int id2 = col + row;
21+
if (cols[col] || d1[id1] || d2[id2]) continue;
22+
cols[col] = d1[id1] = d2[id2] = true;
23+
Solve(row + 1, n, cols, d1, d2, ref count);
24+
cols[col] = d1[id1] = d2[id2] = false;
25+
}
26+
}
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
52\. N-Queens II
2+
3+
Hard
4+
5+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
6+
7+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
12+
13+
**Input:** n = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 1
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 9`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
3+
namespace LeetCodeNet.G0001_0100.S0054_spiral_matrix {
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Simulation
6+
// #Programming_Skills_II_Day_8 #Level_2_Day_1_Implementation/Simulation #Udemy_2D_Arrays/Matrix
7+
// #Top_Interview_150_Matrix #2025_07_01_Time_0_ms_(100.00%)_Space_46.80_MB_(7.91%)
8+
9+
public class Solution {
10+
public IList<int> SpiralOrder(int[][] matrix) {
11+
var res = new List<int>();
12+
if (matrix == null || matrix.Length == 0) return res;
13+
int m = matrix.Length, n = matrix[0].Length;
14+
int top = 0, bottom = m - 1, left = 0, right = n - 1;
15+
while (top <= bottom && left <= right) {
16+
for (int j = left; j <= right; j++) res.Add(matrix[top][j]);
17+
top++;
18+
for (int i = top; i <= bottom; i++) res.Add(matrix[i][right]);
19+
right--;
20+
if (top <= bottom) {
21+
for (int j = right; j >= left; j--) res.Add(matrix[bottom][j]);
22+
bottom--;
23+
}
24+
if (left <= right) {
25+
for (int i = bottom; i >= top; i--) res.Add(matrix[i][left]);
26+
left++;
27+
}
28+
}
29+
return res;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)