标签:end bsp div stringbu har dsa rev else nbsp
一个prev保存前一个状态,一个cur保存这次的状态每次按题意操作prev,然后赋值给cur
如何按题意操作呢?
1
11
21
1211
111221
一个变量say保存到目前为止相同的值,直到say和prev[i]不同,就给cur插入say的个数,和say的值,然后更新say为prev[i]
1 class Solution { 2 public String countAndSay(int n) { 3 StringBuilder cur = new StringBuilder("1"); 4 //char say = cur.charAt(0); 5 6 for(int i=1; i<n; i++){ 7 StringBuilder prev = cur; 8 cur = new StringBuilder(); 9 int count = 1; 10 char say = prev.charAt(0); 11 12 for(int j=1; j<prev.length(); j++){ 13 if(prev.charAt(j) != say){ 14 cur.append(count).append(say); 15 count = 1; 16 say = prev.charAt(j); 17 }else{ 18 count++; 19 } 20 } 21 cur.append(count).append(say); 22 } 23 return cur.toString(); 24 } 25 }
标签:end bsp div stringbu har dsa rev else nbsp
原文地址:https://www.cnblogs.com/hwd9654/p/10966587.html