Java Solution


  • 0
    B
    public class Solution {
        public String countAndSay(int n) {
        
            StringBuilder ans = new StringBuilder("1");
            
            StringBuilder temp;
            int count = 1;
            
            
            for(int i = 2; i <= n; i++) {
                
                temp = ans.append("$");
                ans = new StringBuilder();
                
                count = 1;
                char say = temp.charAt(0);
                
                int len = temp.length();
                
                for(int j = 1; j < len; j++) {
                    
                    if(temp.charAt(j) != say) {
                        ans.append(count).append(say);
                        
                        say = temp.charAt(j);
                        count = 1;
                    } else {
                        count++;
                    }    
                }
            }
            
            return ans.toString();
        }
    }
    

Log in to reply
 

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