LeetCode 38 Count and Say
来源:互联网 发布:南风知我意琰阙 编辑:程序博客网 时间:2024/05/29 02:58
题意:
定义一种迭代,1, 11, 21, 1211, 111221, ...,比如1211 -> 111221表示1211从左往右读为1个1、1个2、2个1,求迭代n轮的结果。
思路:
用string暴力模拟迭代过程,不推荐迭代计数x个y这种方式,因为x>9的时候会变成2个数字,用string更好处理。
代码:
class Solution {public: string countAndSay(int n) { string s = "1"; while (--n) { stringstream ss; int cnt = 1; for (int i = 1; i < s.size(); ++i) { if (s[i] != s[i - 1]) { ss << cnt << s[i - 1]; cnt = 1; } else { ++cnt; } } ss << cnt << s[s.size() - 1]; s = ss.str(); } return s; }};
阅读全文
0 0
- [LeetCode 38] Count and Say
- LeetCode(38) Count and Say
- [leetcode 38] Count and Say
- [Leetcode] 38 - Count and Say
- leetcode-38 count and say
- [LeetCode 38]Count and Say
- LeetCode 38 Count and Say
- leetcode-38 Count and Say
- Leetcode 38 Count and Say
- Leetcode---(38)Count and Say
- leetcode 38: Count and Say
- Leetcode# 38 Count and Say
- leetcode[38]:Count and Say
- leetcode-38 Count And Say
- [Leetcode]#38 Count and Say
- LeetCode 38 Count and Say
- leetcode 38:Count and Say
- [LeetCode-38] Count and Say
- margin在文档流和浮动中的区别
- JavaWeb三大组件之一Servlet【Servlet细节】
- 网络获取图片高度和宽度
- iOS Base64编码
- 网上资源交易系统(ssm)
- LeetCode 38 Count and Say
- ES6 Object.assign()
- HTML标签速查列表
- Java实现在线预览附件 office转换PDF
- 查询函数在哪些存储过程中使用
- 6-Python-面向对象
- 全面掌握 Node 命令选项
- iOS Pointer is missing a nullability type specifier (__nonnull or __nullable)
- 51nod 1120 机器人走方格 V3(卡特兰数,lucas定理)