字符串解码
class Solution { public String decodeString(String s) { StringBuilder sb = new StringBuilder(); int i = 0, n = s.length(); while(i < n){ if(s.charAt(i) >= '0' && s.charAt(i) <= '9'){ int j = i+1; while(s.charAt(j) >= '0' && s.charAt(j) <= '9') j++; int repeat = Integer.parseInt(s.substring(i, j)); i = j; j++; int cnt = 1; while(cnt != 0){ if(s.charAt(j) == '[') cnt++; else if(s.charAt(j) == ']') cnt--; j++; } String str = decodeString(s.substring(i+1, j-1)); for(int k = 0; k < repeat; k++) sb.append(str); i = j; }else{ sb.append(s.charAt(i)); i++; } } return sb.toString(); } }