forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
37 lines (37 loc) · 910 Bytes
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Text;
public class Solution {
public string CountAndSay(int n) {
var s = "1";
while (n > 1)
{
var sb = new StringBuilder();
var lastChar = '1';
var count = 0;
foreach (var ch in s)
{
if (count > 0 && lastChar == ch)
{
++count;
}
else
{
if (count > 0)
{
sb.Append(count);
sb.Append(lastChar);
}
lastChar = ch;
count = 1;
}
}
if (count > 0)
{
sb.Append(count);
sb.Append(lastChar);
}
s = sb.ToString();
--n;
}
return s;
}
}