Skip to content

Commit 7094659

Browse files
committed
feat: add csharp solution to lc problem: No.0264. Ugly Number II
1 parent 253d688 commit 7094659

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
- [等差数列划分](./solution/0400-0499/0413.Arithmetic%20Slices/README.md)
183183
- [解码方法](./solution/0000-0099/0091.Decode%20Ways/README.md)
184184
- [不同的二叉搜索树](./solution/0000-0099/0096.Unique%20Binary%20Search%20Trees/README.md)
185+
- [丑数 II](./solution/0200-0299/0264.Ugly%20Number%20II/README.md)
185186
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
186187
- [最小路径和](./solution/0000-0099/0064.Minimum%20Path%20Sum/README.md)
187188
- [最长上升子序列](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
176176
- [Arithmetic Slices](./solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md)
177177
- [Decode Ways](./solution/0000-0099/0091.Decode%20Ways/README_EN.md)
178178
- [Unique Binary Search Trees](./solution/0000-0099/0096.Unique%20Binary%20Search%20Trees/README_EN.md)
179+
- [Ugly Number II](./solution/0200-0299/0264.Ugly%20Number%20II/README_EN.md)
179180
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
180181
- [Longest Increasing Subsequence](./solution/0300-0399/0300.Longest%20Increasing%20Subsequence/README_EN.md)
181182
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)

solution/0200-0299/0264.Ugly Number II/README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Solution:
7171
p3 += 1
7272
if dp[i] == next5:
7373
p5 += 1
74-
return dp[n - 1]
74+
return dp[-1]
7575
```
7676

7777
### **Java**
@@ -174,6 +174,36 @@ func min(a, b int) int {
174174
}
175175
```
176176

177+
### **C#**
178+
179+
```cs
180+
public class Solution {
181+
public int NthUglyNumber(int n) {
182+
int[] dp = new int[n];
183+
dp[0] = 1;
184+
int p2 = 0, p3 = 0, p5 = 0;
185+
for (int i = 1; i < n; ++i)
186+
{
187+
int next2 = dp[p2] * 2, next3 = dp[p3] * 3, next5 = dp[p5] * 5;
188+
dp[i] = Math.Min(next2, Math.Min(next3, next5));
189+
if (dp[i] == next2)
190+
{
191+
++p2;
192+
}
193+
if (dp[i] == next3)
194+
{
195+
++p3;
196+
}
197+
if (dp[i] == next5)
198+
{
199+
++p5;
200+
}
201+
}
202+
return dp[n - 1];
203+
}
204+
}
205+
```
206+
177207
### **...**
178208

179209
```

solution/0200-0299/0264.Ugly Number II/README_EN.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Solution:
5353
p3 += 1
5454
if dp[i] == next5:
5555
p5 += 1
56-
return dp[n - 1]
56+
return dp[-1]
5757
```
5858

5959
### **Java**
@@ -154,6 +154,36 @@ func min(a, b int) int {
154154
}
155155
```
156156

157+
### **C#**
158+
159+
```cs
160+
public class Solution {
161+
public int NthUglyNumber(int n) {
162+
int[] dp = new int[n];
163+
dp[0] = 1;
164+
int p2 = 0, p3 = 0, p5 = 0;
165+
for (int i = 1; i < n; ++i)
166+
{
167+
int next2 = dp[p2] * 2, next3 = dp[p3] * 3, next5 = dp[p5] * 5;
168+
dp[i] = Math.Min(next2, Math.Min(next3, next5));
169+
if (dp[i] == next2)
170+
{
171+
++p2;
172+
}
173+
if (dp[i] == next3)
174+
{
175+
++p3;
176+
}
177+
if (dp[i] == next5)
178+
{
179+
++p5;
180+
}
181+
}
182+
return dp[n - 1];
183+
}
184+
}
185+
```
186+
157187
### **...**
158188

159189
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int NthUglyNumber(int n) {
3+
int[] dp = new int[n];
4+
dp[0] = 1;
5+
int p2 = 0, p3 = 0, p5 = 0;
6+
for (int i = 1; i < n; ++i)
7+
{
8+
int next2 = dp[p2] * 2, next3 = dp[p3] * 3, next5 = dp[p5] * 5;
9+
dp[i] = Math.Min(next2, Math.Min(next3, next5));
10+
if (dp[i] == next2)
11+
{
12+
++p2;
13+
}
14+
if (dp[i] == next3)
15+
{
16+
++p3;
17+
}
18+
if (dp[i] == next5)
19+
{
20+
++p5;
21+
}
22+
}
23+
return dp[n - 1];
24+
}
25+
}

0 commit comments

Comments
 (0)