My 2ms java Solution


  • 1
    public class Solution {
        public String countAndSay(int n) {
            String str = "1";
    		for(int i=0;i<n-1;i++){
    			str = calculate(str);
    		}
    		return str;
    	}
    	public String calculate(String str){
    		int n = str.length();
    		int count = 1;
    		StringBuilder sb = new StringBuilder();
    		for(int j=0;j<n;j++){
    			if((j+1<n)&&str.charAt(j)==str.charAt(j+1)){
    				count++;
    			}else{
    				sb.append(count);
    				sb.append(str.charAt(j));
    				count=1;
    			}
    		}
    		return sb.toString();
    	}
    }

Log in to reply
 

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