38. Count and Say
来源:互联网 发布:js去掉前面的空格 编辑:程序博客网 时间:2024/06/05 00:11
这道题开始开解错了题意,目标是求第n个数是几,而它是跟第n-1个数有关的
非递归法
class Solution {public: string countAndSay(int n) { string s="1"; for(int i=0;i<n-1;i++) { int count=1; int num=0,j; char *a=new char[10000]; for(j=0;j+1<s.size();j++) { if(s[j]==s[j+1]) count++; else { a[num++]=count%10+'0'; count=count/10; a[num++]=s[j]; count=1; } } a[num++]=count%10+'0'; count=count/10; a[num++]=s[j]; a[num]='\0'; s=a; delete []a; } return s; }};
递归法
class Solution {public: string countAndSay(int n) { if(1==n) return "1"; string s=countAndSay(n-1); string temp=""; int count=1,j; for(j=0;j+1<s.size();j++) { if(s[j]==s[j+1]) count++; else { char c=count%10+'0'; temp=temp+c; temp=temp+s[j]; count=1; } } char c=count%10+'0'; temp=temp+c; temp=temp+s[j]; return temp; }};
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 【神经网络与深度学习】学习笔记:AlexNet&Imagenet学习笔记
- 计算机网络
- 【第三周】第三章习题3.7
- python3.4爬虫——urllib等模块的引用
- 求单链表的长度(可能有环)
- 38. Count and Say
- GDB调试
- 线性表的归并排序
- Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
- 【神经网络与深度学习】卷积神经网络-进化史:从LeNet到AlexNet
- OC-检测UITextView输入内容字数加限制
- 道客web前端开发实习生,电话面试问题
- Java Study 04
- google hacking 常用语句整合