c# solution


  • 0
    N
    public class Solution {
    	public string CountAndSay(int n) {
            var list= new List<char>();
            list.Add('1');
            
            for(var i=1;i<n;i++){
                var l = new List<char>();
    			var current = ' ';
    			var count = 0;
    
                for(var j=0;j<list.Count;j++) {
                    if(list[j]!=current){
                        current = list[j];
                        count = 1;
                    }
                    else {
                        count++;
                    }
                    
                    if(j==(list.Count-1) || list[j+1]!= current){
                        Console.WriteLine(current);
                        l.AddRange(string.Format("{0}{1}", count, current));
                    }
                }
                list = l;
    			
    			Console.WriteLine(string.Join("",list));
    
            }
            
            return string.Join(string.Empty,list);
        }
    }
    

Log in to reply
 

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