-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path038-CountAndSay.cs
More file actions
44 lines (39 loc) · 1.22 KB
/
038-CountAndSay.cs
File metadata and controls
44 lines (39 loc) · 1.22 KB
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
38
39
40
41
42
43
44
//-----------------------------------------------------------------------------
// Runtime: 80ms
// Memory Usage: 22.8 MB
// Link: https://leetcode.com/submissions/detail/352429668/
//-----------------------------------------------------------------------------
using System.Text;
namespace LeetCode
{
public class _038_CountAndSay
{
public string CountAndSay(int n)
{
var result = "1";
char currentCh;
int i, j, startNum;
var builder = new StringBuilder();
for (i = 1; i < n; i++)
{
currentCh = result[0];
startNum = 0;
for (j = 1; j < result.Length; j++)
{
if (currentCh != result[j])
{
builder.Append(j - startNum);
builder.Append(currentCh);
currentCh = result[j];
startNum = j;
}
}
builder.Append(j - startNum);
builder.Append(currentCh);
result = builder.ToString();
builder.Clear();
}
return result;
}
}
}