博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Count and Say
阅读量:7056 次
发布时间:2019-06-28

本文共 1314 字,大约阅读时间需要 4 分钟。

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" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

数字转换为string,使用stringstream流,字符直接使用append追加到string后面。注意,对于流,每次用完要清空。

 

C++代码实现:

#include
#include
#include
using namespace std;class Solution{public: string countAndSay(int n) { if(n==0) return NULL; if(n==1) return string("1"); string s="1"; int i; size_t j,k=0; for(i=2; i<=n; i++) { string tmp; stringstream ss; for(j=1; j

运行结果:

 改进版
#include
#include
#include
using namespace std;class Solution{public: string countAndSay(int n) { if(n==0) return NULL; if(n==1) return string("1"); string s="1"; int i; size_t j,k=0; for(i=2; i<=n; i++) { string tmp(s); j=0; int m=tmp.size(); s.clear(); while(j

 

转载地址:http://kmool.baihongyu.com/

你可能感兴趣的文章
解题思路:蓄水池问题
查看>>
android 实时显示系统时间
查看>>
fatal error: asm/system.h: No such file or directory
查看>>
为什么要设计
查看>>
SerializableObj
查看>>
2018年5月31日笔记
查看>>
(转)CentOs上配置samba服务
查看>>
Photoshop给草坪上的人物加上唯美的紫色霞光
查看>>
移动平台对 META 标签的定义
查看>>
curl 命令详解
查看>>
启动改为本地Ip
查看>>
云服务器CentOS7.5安装MySQL5.7
查看>>
代理模式
查看>>
vue文档阅读笔记——计算属性和侦听器
查看>>
单元测试系列:Mock工具之Mockito实战
查看>>
二手GTX650
查看>>
Guava学习-缓存
查看>>
hexSHA1散列加密解密(不可逆)
查看>>
Pinterest架构:两年内月PV从零到百亿 - 非常值得参考【转】 - HorsonJin - 博客园...
查看>>
Swift设置自动行高
查看>>