simple java solution


  • 0
    S
     public String countAndSay(int num) {
            String base = "1";
    		for (int i = 1; i < num; i++) {
    			int count = 0;
    			String result = "";
    			while (count < base.length()) {
    				int times = 1;
    				while (count + 1 < base.length() && base.charAt(count) == base.charAt(count + 1)) {
    					count++;
    					times++;
    				}
    				count++;
    				result += "" + times + base.charAt(count - 1);
    			}
    			base = result == "" ? base : result;
    		}
    
    		return base;
        }
    

Log in to reply
 

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