My solution too slow C#


  • 0
    D
    public static string CountAndSay(int n)
        {
            string ret="11";
            if (n == null) return null;
            if (n == 1) return "1";
            if (n == 2) return "11";
            else{
                for (int i = 3; i <= n; ++i)
                {
                    ret = helper(ret);
                }
            }
            return ret;
        }
    
        public static string helper(string input)
        {
            string temp = input.Substring(0, 1);
            int count = 1;
            string ret = "";
            for (int i = 1; i < input.Length; ++i)
            {
                if (input.Substring(i, 1) == temp) count++;
                else
                {
                    ret += count.ToString() + temp;
                    temp = input.Substring(i, 1);
                    count=1;
                }
            }
            ret += count.ToString() + temp;
            return ret;
        }

Log in to reply
 

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