To generate then term, justcount and saythen-1 term. 总之就是第n条结果依赖于第n-1条结果。例如: 结果3=21,结果4=1211,从结果3读出1个2和1个1,所以结果4=1211。 话不多说直接上代码吧: 解题代码: classSolution{public:stringcountAndSay(intn){intcount,getnum; string res="1";if(n==1)retu...
Leetcode 题目整理-8 Count and Say 38. Count and Say 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 1s" or 21. 21 is read off as "one 2, then one 1" ...
https://leetcode.com/problems/count-and-say/description/leetcode.com/problems/count-and-say/description/ 解题思路 过程捋清楚应该就比较好做 class Solution { public: string countAndSay(int n) { if(n == 1) return string("1"); string input = countAndSay(n - 1); string result = "...
首先这个题目肯定要用递归来实现,所以: 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+'%d%s'%(r,c))r...
class Solution{public:stringcountAndSay(intn){if(n==1)return"1";string temp=countAndSay(n-1);string result="";for(inti=0;i<temp.size();i++){intcount=1;while(i<temp.length()-1and temp[i+1]==temp[i]){++i;++count;}result+=to_string(count)+temp[i];}returnresult;}}; ...
public class Solution { public String countAndSay(int n) { //require if(n<=0) return null; return generator(n); } public String generator(int n){ //require String str="1"; //invariant for(int i=1;i<n;i++){ if(str.length()<=1){ ...
Great Solution publicclassSolution{publicStringcountAndSay(intn){StringBuildercurr=newStringBuilder("1");StringBuilderprev;intcount;charsay;for(inti=1;i<n;i++){prev=curr;curr=newStringBuilder();count=1;say=prev.charAt(0);for(intj=1,len=prev.length();j<len;j++){if(prev.charAt(j)!=say)...
原题链接:https://leetcode.com/problems/count-and-say/ class Solution { public: string countAndSay(int n) { string s = "1"; while(--n) { int l = s.size(); string ns = ""; for (int j = 0; j < l;) { int idx = j; while (idx < l && s[j] == s[idx]) { idx...
package leetcode_50; /*** * * @author pengfei_zheng * 按照规律进行求解字符串 */ public class Solution38 { public static String countAndSay(int n) { if(n<=0) { return ""; } String s="1"; int times = 1; while(times<n){ ...
LeetCode(38):外观数列 给定一个正整数 n ,输出外观数列的第 n 项。 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。 你可以将其视作是由递归公式定义的数字字符串序列: countAndSay(1) = “1” countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字...