My Java Solution


  • 0
    F
    public String countAndSay(int n) {
    	StringBuilder res = new StringBuilder("1");
    	for (int i = 2; i <= n; i++) {
    		int j = 0, count = 1;
    		StringBuilder temp = new StringBuilder();
    		while (j < res.length()) {
    			char say = res.charAt(j);
    			while (j + 1 < res.length() && say == res.charAt(j + 1)) {
    				count++;
    				j++;
    			}
    			temp.append(count).append(say);
    			j++;
    			count = 1;
    		}
    		res = temp;
    	}
    	return res.toString();
    }

Log in to reply
 

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