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

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

Description:

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.

分析:这道题目就是一模拟,从1开始,根据读音来产生新的序列,计算第n个字符串是什么。这里主要是一点:在写代码的时候

觉得有重复的部分,臃肿了一些,然后再室友的指导下,加了一个标示字符使代码更简洁。两个都贴上

1 class Solution { 2 public: 3     string countAndSay(int n) { 4         string nows = "1"; 5         stringstream itos; 6          7         for(int i=2;i<=n;i++) 8         { 9             char nowc = nows[0];10             int count =1;11             string read,scount;12             for(int j=1;j
>scount;19 itos.clear();20 read = read+scount+nowc;21 count = 1;22 nowc = nows[j];23 }24 }25 itos<
>scount;27 itos.clear();28 nows = read +scount +nowc;29 30 }31 return nows;32 }33 };

 

1 class Solution { 2 public: 3     string countAndSay(int n) { 4         string nows = "1e"; 5         stringstream itos; 6          7         for(int i=2;i<=n;i++) 8         { 9             char nowc = nows[0];10             int count =1;11             string read,scount;12             for(int j=1;j
>scount;19 itos.clear();20 read = read+scount+nowc;21 count = 1;22 nowc = nows[j];23 }24 }25 nows = read +'e';26 27 }28 string result(nows.begin(),nows.end()-1);29 return result;30 }31 };

 

转载于:https://www.cnblogs.com/soyscut/p/3787564.html

你可能感兴趣的文章
笔记一:python安装和执行
查看>>
api工程IOS学习:在IOS开发中使用GoogleMaps SDK
查看>>
函数功能MATLAB
查看>>
Bzoj1123 Blockade
查看>>
Python之Mysql及SQLAlchemy操作总结
查看>>
数据库搜索与索引
查看>>
python3 面向对象(一)
查看>>
配件商城项目总结
查看>>
关于变量名前面加m的问题
查看>>
腾讯Bugly异常崩溃SDK接入
查看>>
安装centos后无法引导启动windows7的解决方法
查看>>
AutoMapper用法
查看>>
Java 学习笔记(4)——java 常见类
查看>>
IOS开源项目汇总
查看>>
用herl工具解决微信内链接或二维码可直接用外部浏览器打开
查看>>
GITHup的使用
查看>>
void main()是错的!
查看>>
Atitit. Attilax企业框架 AEF的发展里程总结
查看>>
亚麻 面经_ml
查看>>
豆瓣api
查看>>