}stringcountAndSay(intn){stringres ="1";while(--n) { res = read(res); }returnres; } 解法2:调整了解法1中read函数的处理逻辑。同样从左往右遍历一遍,若与后一个数相同,计数值count加1;若不同,将计数值和当前位置数值插入字符串,同时重置计数值。为了防止最后一位比较时出现错误,在原字符串末尾添加...
stringSolution::countAndSay(intn) {stringresult,str_temp;if(n==0) {returnNULL;}if(n==1) { result.push_back(1+'0');returnresult; }intn_copy; n_copy=n;//while (n)//{//string temp_a;//temp_a.push_back(n%10+'0');//str_temp.insert(0,temp_a);//n=n/10;//}str_temp...
public String countAndSay(int n) { String r = "1"; for (int i = 1; i < n; i++) { String t = ""; int count = 0; String flag = r.charAt(0) + ""; for (int j = 0; j < r.length(); j++) { if ((r.charAt(j) + "").equals(flag)) { count++; } else { t...
实现代码: publicstringCountAndSay(intn){if(n<=0){returnstring.Empty;}if(n==1){return"1";}n=n-1;varresult="1";for(vari=0;i<n;i++){varr=string.Empty;varlen=result.Length;varcount=1;for(varj=1;j<len+1;j++){if(j<len&&result[j]==result[j-1]){count++;}else{r+=string...
下面我们来写代码吧。首先这个题目肯定要用递归来实现,所以: classSolution:defcountAndSay(self,n):""":type n: int:rtype: str"""ifn<=1:return'1'defcount(s,last=''):ifs=='':returnlastc=s[0]forrinrange(1,3):if(r>=len(s))or(s[r]!=c):breakelse:r+=1returncount(s[r:],last...
这道计数和读法问题还是第一次遇到,看似挺复杂,其实仔细一看,算法很简单,就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。代码如下: classSolution { public:stringcountAndSay(intn) {if(n <=0)return"";stringres ="1";while(--n) {stringcur ="";for(inti =0; i < res...
因为第n个数count and say的结果是基于第n-1个数的,我们可以用递归解决这个问题。 代码 public class Solution { public String countAndSay(int n) { if(n == 0){ return ""; } if(n == 1){ return "1"; } String s = countAndSay(n-1); ...
countAndSay(2) = RLE of "1" = "11" countAndSay(3) = RLE of "11" = "21" countAndSay(4) = RLE of "21" = "1211" Example 2: Input:n = 1 Output:"1" Explanation: This is the base case. Constraints: 1 <= n <= 30 ...
If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. spoilers alert… click to ...
LeetCode 38 Count and Say(计数与报数) 。备注:数字序列应该用字符串表示。</code> 原文 <code class="hljs applescript">The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two ...