Skip to content

Commit 7f8f9d4

Browse files
authored
Improved tasks 30, 50, 54, 57, 58
1 parent 705b877 commit 7f8f9d4

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

LeetCodeNet/G0001_0100/S0030_substring_with_concatenation_of_all_words/Solution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Collections.Generic;
2-
31
namespace LeetCodeNet.G0001_0100.S0030_substring_with_concatenation_of_all_words {
42

53
// #Hard #String #Hash_Table #Sliding_Window #Top_Interview_150_Sliding_Window
64
// #2025_06_30_Time_15_ms_(94.14%)_Space_57.94_MB_(46.58%)
75

6+
using System.Collections.Generic;
7+
88
public class Solution {
99
public IList<int> FindSubstring(string s, string[] words) {
1010
var res = new List<int>();
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
namespace LeetCodeNet.G0001_0100.S0050_powx_n {
22

33
// #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%)
4+
// #2025_07_02_Time_0_ms_(100.00%)_Space_29.32_MB_(55.95%)
55

66
public class Solution {
77
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;
8+
long nn = n;
9+
double res = 1;
10+
if (n < 0) {
11+
nn *= -1;
1312
}
14-
double result = 1.0;
15-
while (N > 0) {
16-
if ((N & 1) == 1) result *= x;
17-
x *= x;
18-
N >>= 1;
13+
while (nn > 0) {
14+
if ((nn % 2) == 1) {
15+
nn--;
16+
res *= x;
17+
} else {
18+
x *= x;
19+
nn /= 2;
20+
}
1921
}
20-
return result;
22+
if (n < 0) {
23+
return 1.0 / res;
24+
}
25+
return res;
2126
}
2227
}
2328
}

LeetCodeNet/G0001_0100/S0054_spiral_matrix/Solution.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
using System.Collections.Generic;
2-
31
namespace LeetCodeNet.G0001_0100.S0054_spiral_matrix {
42

53
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Simulation
64
// #Programming_Skills_II_Day_8 #Level_2_Day_1_Implementation/Simulation #Udemy_2D_Arrays/Matrix
75
// #Top_Interview_150_Matrix #2025_07_01_Time_0_ms_(100.00%)_Space_46.80_MB_(7.91%)
86

7+
using System.Collections.Generic;
8+
99
public class Solution {
1010
public IList<int> SpiralOrder(int[][] matrix) {
1111
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;
12+
if (matrix == null || matrix.Length == 0) {
13+
return res;
14+
}
15+
int m = matrix.Length;
16+
int n = matrix[0].Length;
17+
int top = 0;
18+
int bottom = m - 1;
19+
int left = 0;
20+
int right = n - 1;
1521
while (top <= bottom && left <= right) {
16-
for (int j = left; j <= right; j++) res.Add(matrix[top][j]);
22+
for (int j = left; j <= right; j++) {
23+
res.Add(matrix[top][j]);
24+
}
1725
top++;
18-
for (int i = top; i <= bottom; i++) res.Add(matrix[i][right]);
26+
for (int i = top; i <= bottom; i++) {
27+
res.Add(matrix[i][right]);
28+
}
1929
right--;
2030
if (top <= bottom) {
21-
for (int j = right; j >= left; j--) res.Add(matrix[bottom][j]);
31+
for (int j = right; j >= left; j--) {
32+
res.Add(matrix[bottom][j]);
33+
}
2234
bottom--;
2335
}
2436
if (left <= right) {
25-
for (int i = bottom; i >= top; i--) res.Add(matrix[i][left]);
37+
for (int i = bottom; i >= top; i--) {
38+
res.Add(matrix[i][left]);
39+
}
2640
left++;
2741
}
2842
}

LeetCodeNet/G0001_0100/S0057_insert_interval/Solution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Collections.Generic;
2-
31
namespace LeetCodeNet.G0001_0100.S0057_insert_interval {
42

53
// #Medium #Array #Level_2_Day_17_Interval #Top_Interview_150_Intervals
64
// #2025_07_01_Time_1_ms_(99.33%)_Space_51.29_MB_(48.12%)
75

6+
using System.Collections.Generic;
7+
88
public class Solution {
99
public int[][] Insert(int[][] intervals, int[] newInterval) {
1010
var result = new List<int[]>();
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
namespace LeetCodeNet.G0001_0100.S0058_length_of_last_word {
22

33
// #Easy #String #Programming_Skills_II_Day_6 #Udemy_Arrays #Top_Interview_150_Array/String
4-
// #2025_07_01_Time_0_ms_(100.00%)_Space_40.26_MB_(15.20%)
4+
// #2025_07_02_Time_0_ms_(100.00%)_Space_40.17_MB_(24.14%)
55

66
public class Solution {
77
public int LengthOfLastWord(string s) {
8-
int length = 0, i = s.Length - 1;
9-
while (i >= 0 && s[i] == ' ') i--;
10-
while (i >= 0 && s[i] != ' ') {
11-
length++;
12-
i--;
8+
int len = 0;
9+
for (int i = s.Length - 1; i >= 0; i--) {
10+
char ch = s[i];
11+
if (ch == ' ' && len > 0) {
12+
break;
13+
} else if (ch != ' ') {
14+
len++;
15+
}
1316
}
14-
return length;
17+
return len;
1518
}
1619
}
1720
}

0 commit comments

Comments
 (0)