LeetCode 报数
作者:互联网
https://leetcode-cn.com/problems/count-and-say/description/
我的解决方案:
class Solution {
public String solveIt(String str, int deepth) {
if(str == "" || str == null) return "";
if(deepth==0) return str;
String res="";
for(int i=0;i<str.length();i++) {
int index=i;
int cnt=1;
while(i<(str.length()-1)&&str.charAt(i)==str.charAt(i+1)) {
cnt++;
i++;
}
res+=Integer.toString(cnt);
res+=str.substring(index, index+1);
}
return solveIt(res, deepth-1);
}
public String countAndSay(int n) {
//此题目可以使用递归的方式解决
String str="1";
return solveIt(str,n-1);
}
}
标签:return,String,int,报数,deepth,str,LeetCode,cn 来源: https://blog.51cto.com/u_12534050/2953534