C# solution clear


  • 0
    Y
    public class Solution {
        public string CountAndSay(int n) {
            string res="1";
            if(n==1)
                return res;
            for(int i=1;i<n;i++)
            {
                res=countSay(res);
            }
            return res;
        }
        
        public string countSay(string s)
        {
            if(string.IsNullOrEmpty(s))
                return "";
            StringBuilder sb=new StringBuilder();
            int count=1;char cur=s[0];
            for(int i=1;i<s.Length;i++)
            {
                if(s[i]==cur)
                    count++;
                else
                {
                    sb.Append(count.ToString());
                    sb.Append(s[i-1]);
                    cur=s[i];
                    count=1;
                }
            }
            sb.Append(count);
            sb.Append(cur);
            return sb.ToString();
        }
    }

Log in to reply
 

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