C# solution


  • 0
    S
    public class Solution {
        public string CountAndSay(int n) {
            StringBuilder curr = new StringBuilder();
            curr.Append('1');		
            StringBuilder next;
    		       
            while(n-- > 1) {
                int count = 1;
    			next = new StringBuilder();
    			for(int i = 1; i < curr.Length; ++i) {
    				if(curr[i] != curr[i - 1]) {
    					next.Append(count);
    					next.Append(curr[i - 1]);
    					count = 1;
    				} else {
    					count += 1;
    				}
    			}
    			
    			next.Append(count);
    			next.Append(curr[curr.Length - 1]);
    
    			curr = next;
            }
            
            return curr.ToString();
            
        }
    }
    

Log in to reply
 

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