A simple C# solution


  • 0
    A
        public string CountAndSay(int n) {
            if(n<=1)
                return n.ToString();
            
            String str = "11";
            String result = String.Empty;
            
            
            for(int i = 2; i < n; i++)
            {
                Char prev = str[0];
                int count = 1;
                
                for(int j = 1; j < str.Length; j++)
                {
                    if(str[j] == prev)
                        count++;
                    else
                    {
                        result += count.ToString() + prev.ToString();
                        count = 1;
                    }
                    
                    prev = str[j];
                }
                result += count.ToString() + prev.ToString();
                str = result;
                result = String.Empty;
            }
            
            return str;
        }
    

Log in to reply
 

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