改进:
public String countAndSay1(int n) { if (n == 1) { return "1"; } String s = "1"; StringBuffer buffer = new StringBuffer(); int count = 0; int i; for (int round = 1; round < n; round++) { count = 1; buffer.setLength(0); for (i = 1; i < s.length(); i++) { if (s.charAt(i) == s.charAt(i-1)) { count++; } else { buffer.append(count).append(s.charAt(i-1)); count = 1; } } buffer.append(count).append(s.charAt(i-1)); s = buffer.toString(); } return buffer.toString(); }//使用StringBuffer.append;
新闻热点
疑难解答