public class Solution {
public string CountAndSay(int n) {
//1
//11
//21
//1211
//111221
//312211
//13112221
//1113213211
if (n == 1)
{
return "1";
}
else
{
var list = new List<string>();
list.Add("1");
var result = "";
var pre = "";
for (int i = 1; i < n; i++)
{
pre = list[i - 1];
var c = pre[0];
var newrow = new StringBuilder();
var count = 1;
for (int j = 1; j < pre.Length; j++)
{
var cur = pre[j];
if (c != cur)
{
newrow.Append(count).Append(c);
count = 1;
c = cur;
}
else
{
count++;
}
}
newrow.Append(count).Append(c);
list.Add(newrow.ToString());
}
result = list[list.Count - 1];
return result;
}
}
}
https://leetcode.com/problems/count-and-say/#/description