C# 53ms DP Solution


  • 0
    L
    public class Solution {
        public string CountAndSay(int n) {
            if(n == 1) return "1";
            int j = 2;
            string str = "1";
            while(j <= n)
            {
                StringBuilder tmpSB = new StringBuilder();
                for(int i = 0; i < str.Length; i++)
                {
                    char curNum = str[i];
                    int len = 1;
                    while(i < str.Length - 1 && str[i] == str[i+1])
                    {
                        i++;
                        len++;
                    }
                    tmpSB.Append(len.ToString() + curNum.ToString());
                }
                str = tmpSB.ToString();
                j++;
            }
            return str;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.