leetcode-38 count-and-say(报数)
这道题我认为并不是特别的好,非常难以理解提议,leetcode很多人也抗议这道题的题意表述,而且我也想不出来解决的方法。先看一下描述:
我来解释一下,其实就是解释上一项的意思。第一项是1,第二项是描述第一项“1个1”,所以是11,第三项描述第二项“2个1”,所以是21,以此类推。上代码:
1 public static String countAndSay(int n) {
2 String oldString = "1";
3 while (--n > 0) {
4 StringBuilder sb = new StringBuilder();
5 char [] oldChars = oldString.toCharArray();
6
7 for (int i = 0; i < oldChars.length; i++) {
8 int count = 1;
9 while ((i+1) < oldChars.length && oldChars[i] == oldChars[i+1]) {
10 count++;
11 i++;
12 }
13 sb.append(String.valueOf(count) + String.valueOf(oldChars[i]));
14 }
15 oldString = sb.toString();
16 }
17
18 return oldString;
19 }

浙公网安备 33010602011771号