Skip to content

Commit dbcfe1b

Browse files
committed
C# code: Formatted and use code style.
1 parent b51b8aa commit dbcfe1b

24 files changed

Lines changed: 305 additions & 180 deletions

‎problems/0053-maximum-subarray.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ These five steps are a pattern for solving `dynamic programming` problems.
8686

8787
## C#
8888
```c#
89-
public class Solution {
90-
public int MaxSubArray(int[] nums) {
91-
var dp = (int[]) nums.Clone();
92-
93-
for (var i = 1; i < dp.Length; i++) {
89+
public class Solution
90+
{
91+
public int MaxSubArray(int[] nums)
92+
{
93+
var dp = (int[])nums.Clone();
94+
95+
for (var i = 1; i < dp.Length; i++)
96+
{
9497
dp[i] = Math.Max(nums[i], dp[i - 1] + nums[i]);
9598
}
9699

‎problems/0072-edit-distance.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,28 @@ class Solution {
159159

160160
## C#
161161
```c#
162-
public class Solution {
163-
public int MinDistance(string word1, string word2) {
162+
public class Solution
163+
{
164+
public int MinDistance(string word1, string word2)
165+
{
164166
var dp = new int[word1.Length + 1, word2.Length + 1];
165-
for (var i = 0; i < dp.GetLength(0); i++) {
167+
168+
for (var i = 0; i < dp.GetLength(0); i++)
166169
dp[i, 0] = i;
167-
}
168-
for (var j = 0; j < dp.GetLength(1); j++) {
170+
171+
for (var j = 0; j < dp.GetLength(1); j++)
169172
dp[0, j] = j;
170-
}
171173

172-
for (var i = 1; i < dp.GetLength(0); i++) {
173-
for (var j = 1; j < dp.GetLength(1); j++) {
174-
if (word1[i - 1] == word2[j - 1]) {
174+
for (var i = 1; i < dp.GetLength(0); i++)
175+
{
176+
for (var j = 1; j < dp.GetLength(1); j++)
177+
{
178+
if (word1[i - 1] == word2[j - 1])
179+
{
175180
dp[i, j] = dp[i - 1, j - 1];
176-
} else {
181+
}
182+
else
183+
{
177184
dp[i, j] = Math.Min(dp[i - 1, j - 1], Math.Min(dp[i - 1, j], dp[i, j - 1])) + 1;
178185
}
179186
}

‎problems/0084-largest-rectangle-in-histogram.md‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ Output: 4
2222
```
2323

2424
### Constraints
25-
```
26-
1 <= heights.length <= 100000
27-
0 <= heights[i] <= 10000
28-
------------------------------------------------------------------------
29-
```
25+
- `1 <= heights.length <= 100000`
26+
- `0 <= heights[i] <= 10000`
3027

3128
## Thoughts
3229
This problem can be solved using **Monotonic Stack**.
@@ -167,23 +164,28 @@ var largestRectangleArea = function(heights) {
167164

168165
## C#
169166
```c#
170-
public class Solution {
171-
public int LargestRectangleArea(int[] heights) {
172-
var maxArea = 0;
167+
public class Solution
168+
{
169+
public int LargestRectangleArea(int[] heights)
170+
{
171+
int maxArea = 0;
173172
var indexStack = new Stack<int>();
174173
heights = [0, ..heights, 0];
175174

176-
for (var i = 0; i < heights.Length; i++) {
177-
while (indexStack.Count > 0 && heights[i] < heights[indexStack.Peek()]) {
178-
var poppedIndex = indexStack.Pop();
175+
for (var i = 0; i < heights.Length; i++)
176+
{
177+
while (indexStack.Count > 0 && heights[i] < heights[indexStack.Peek()])
178+
{
179+
int poppedIndex = indexStack.Pop();
179180

180-
var poppedHeight = heights[poppedIndex];
181-
var leftIndex = indexStack.Peek(); // poppedHeight's remaining left heights are all shorter than it, because when 'poppedHeight' itself was pushed into stack, it must have caused some (could be none) taller heights been popped out of the stack.
182-
var rightIndex = i; // poppedHeight's right heights (which are all taller than 'poppedHeight') have been popped out of the stack (disappeared) when current `i` height is being pushed into stack.
183-
var width = rightIndex - leftIndex - 1; // So in the range of 'width', they are all no shorter than `poppedHeight`, although they have been popped out of the stack (disappeared).
181+
int poppedHeight = heights[poppedIndex];
182+
int leftIndex = indexStack.Peek(); // poppedHeight's remaining left heights are all shorter than it, because when 'poppedHeight' itself was pushed into stack, it must have caused some (could be none) taller heights been popped out of the stack.
183+
int rightIndex = i; // poppedHeight's right heights (which are all taller than 'poppedHeight') have been popped out of the stack (disappeared) when current `i` height is being pushed into stack.
184+
int width = rightIndex - leftIndex - 1; // So in the range of 'width', they are all no shorter than `poppedHeight`, although they have been popped out of the stack (disappeared).
184185
185-
var area = poppedHeight * width;
186-
if (area > maxArea) {
186+
int area = poppedHeight * width;
187+
if (area > maxArea)
188+
{
187189
maxArea = area;
188190
}
189191
}

‎problems/0139-word-break.md‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,25 @@ Detailed solutions will be given later, and now only the best practices in 7 lan
4242

4343
## C#
4444
```c#
45-
public class Solution {
46-
public bool WordBreak(string s, IList<string> wordDict) {
47-
var dp = Enumerable.Repeat(false, s.Length + 1).ToArray();
45+
public class Solution
46+
{
47+
public bool WordBreak(string s, IList<string> wordDict)
48+
{
49+
var dp = new bool[s.Length + 1];
4850
dp[0] = true;
4951

50-
for (var i = 1; i < dp.Length; i++) {
51-
foreach (var word in wordDict) {
52-
if (dp[i]) {
52+
for (var i = 1; i < dp.Length; i++)
53+
{
54+
foreach (var word in wordDict)
55+
{
56+
if (dp[i])
57+
{
5358
break;
5459
}
55-
if (i >= word.Length) {
56-
dp[i] = dp[i - word.Length] &&
57-
word == s[(i - word.Length)..i];
60+
61+
if (i >= word.Length)
62+
{
63+
dp[i] = dp[i - word.Length] && word == s[(i - word.Length)..i];
5864
}
5965
}
6066
}

‎problems/0198-house-robber.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ Detailed solutions will be given later, and now only the best practices in 7 lan
3838

3939
## C#
4040
```c#
41-
public class Solution {
42-
public int Rob(int[] nums) {
43-
if (nums.Length == 1) {
41+
public class Solution
42+
{
43+
public int Rob(int[] nums)
44+
{
45+
if (nums.Length == 1)
4446
return nums[0];
45-
}
4647

4748
var dp = new int[nums.Length];
4849
dp[0] = nums[0];
4950
dp[1] = Math.Max(nums[0], nums[1]);
5051

51-
for (var i = 2; i < dp.Length; i++) {
52+
for (var i = 2; i < dp.Length; i++)
53+
{
5254
dp[i] = Math.Max(dp[i - 1], dp[i - 2] + nums[i]);
5355
}
5456

‎problems/0279-perfect-squares.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ Detailed solutions will be given later, and now only the best practices in 7 lan
3636

3737
## C#
3838
```c#
39-
public class Solution {
40-
public int NumSquares(int n) {
41-
var defaultValue = n + 2; // As long as the value is greater than 'n', it doesn't matter how much it is.
42-
var dp = Enumerable.Repeat(defaultValue, n + 1).ToArray();
39+
public class Solution
40+
{
41+
public int NumSquares(int n)
42+
{
43+
int defaultValue = n + 2; // As long as the value is greater than 'n', it doesn't matter how much it is.
44+
int[] dp = Enumerable.Repeat(defaultValue, n + 1).ToArray();
4345
dp[0] = 0;
4446

45-
for (var i = 1; i < dp.Length; i++) {
46-
for (var j = 1; j * j <= i; j++) {
47+
for (var i = 1; i < dp.Length; i++)
48+
{
49+
for (var j = 1; j * j <= i; j++)
50+
{
4751
dp[i] = Math.Min(dp[i], dp[i - j * j] + 1);
4852
}
4953
}

‎problems/0300-longest-increasing-subsequence.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ Detailed solutions will be given later, and now only the best practices in 4 to
4343
```c#
4444
// 10, 9, 2, 5, 3, 7, 4, 3,101,18 # nums
4545
// 1, 1, 1, 2, 2, 3, 3, 2, 4,4 # dp
46-
public class Solution {
47-
public int LengthOfLIS(int[] nums) {
46+
public class Solution
47+
{
48+
public int LengthOfLIS(int[] nums)
49+
{
4850
var dp = new int[nums.Length];
4951
Array.Fill(dp, 1);
5052

51-
for (var i = 1; i < nums.Length; i++) {
52-
for (var j = i - 1; j >= 0; j--) {
53-
if (nums[i] > nums[j]) {
53+
for (var i = 1; i < nums.Length; i++)
54+
{
55+
for (var j = i - 1; j >= 0; j--)
56+
{
57+
if (nums[i] > nums[j])
58+
{
5459
dp[i] = Math.Max(dp[i], dp[j] + 1);
5560
}
5661
}

‎problems/0322-coin-change.md‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,28 @@ Detailed solutions will be given later, and now only the best practices in 7 lan
4141

4242
## C#
4343
```c#
44-
public class Solution {
45-
public int CoinChange(int[] coins, int amount) {
46-
var defaultValue = amount + 2; // As long as the value is greater than 'amount', it doesn't matter how much it is.
47-
var dp = Enumerable.Repeat(defaultValue, amount + 1).ToArray();
44+
public class Solution
45+
{
46+
public int CoinChange(int[] coins, int amount)
47+
{
48+
int defaultValue = amount + 2; // As long as the value is greater than 'amount', it doesn't matter how much it is.
49+
int[] dp = Enumerable.Repeat(defaultValue, amount + 1).ToArray();
4850
dp[0] = 0;
4951

50-
for (var i = 1; i < dp.Length; i++) {
51-
foreach (var coin in coins) {
52-
if (i >= coin) {
52+
for (var i = 1; i < dp.Length; i++)
53+
{
54+
foreach (int coin in coins)
55+
{
56+
if (i >= coin)
57+
{
5358
dp[i] = Math.Min(dp[i], dp[i - coin] + 1);
5459
}
5560
}
5661
}
5762

58-
if (dp.Last() == defaultValue) {
63+
if (dp.Last() == defaultValue)
5964
return -1;
60-
}
65+
6166
return dp.Last();
6267
}
6368
}

‎problems/0377-combination-sum-iv.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ Detailed solutions will be given later, and now only the best practices in 7 lan
5050

5151
## C#
5252
```c#
53-
public class Solution {
54-
public int CombinationSum4(int[] nums, int target) {
53+
public class Solution
54+
{
55+
public int CombinationSum4(int[] nums, int target)
56+
{
5557
var dp = new int[target + 1];
5658
dp[0] = 1;
57-
58-
for (var i = 1; i < dp.Length; i++) {
59-
foreach (var num in nums) {
60-
if (i >= num) {
59+
60+
for (var i = 1; i < dp.Length; i++)
61+
{
62+
foreach (var num in nums)
63+
{
64+
if (i >= num)
65+
{
6166
dp[i] += dp[i - num];
6267
}
6368
}

‎problems/0392-is-subsequence.md‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,24 @@ class Solution {
111111

112112
## C#
113113
```c#
114-
public class Solution {
115-
public bool IsSubsequence(string s, string t) {
114+
public class Solution
115+
{
116+
public bool IsSubsequence(string s, string t)
117+
{
116118
var dp = new bool[s.Length + 1, t.Length + 1];
117-
for (var j = 0; j < dp.GetLength(1); j++) {
119+
for (var j = 0; j < dp.GetLength(1); j++)
118120
dp[0, j] = true;
119-
}
120121

121-
for (var i = 1; i < dp.GetLength(0); i++) {
122-
for (var j = 1; j < dp.GetLength(1); j++) {
123-
if (s[i - 1] == t[j - 1]) {
122+
for (var i = 1; i < dp.GetLength(0); i++)
123+
{
124+
for (var j = 1; j < dp.GetLength(1); j++)
125+
{
126+
if (s[i - 1] == t[j - 1])
127+
{
124128
dp[i, j] = dp[i - 1, j - 1];
125-
} else {
129+
}
130+
else
131+
{
126132
dp[i, j] = dp[i, j - 1];
127133
}
128134
}

0 commit comments

Comments
 (0)