Count and Say
来源:互联网 发布:在线报名系统 php 编辑:程序博客网 时间:2024/06/07 08:21
题目:
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、分别输入有几个连续的字符count和字符就可以了;
代码:
class Solution {public: string unguarded_convert(const string &say){ stringstream ss; int count = 0; char last = say[0]; for (size_t i = 0; i <= say.size(); ++i) { if (say[i] == last) { ++count; } else { ss << count << last; count = 1; last = say[i]; } } return ss.str();}string countAndSay(int n) { if (n <= 0) return string(); string say = "1"; for (int i = 1; i < n; ++i) { say = unguarded_convert(say); } return say;}};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- Count and Say
- [38]Count and Say
- Count and Say
- Count and Say
- Count and Say
- [LeetCode]Count and Say
- Count and Say
- Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode中的Isomorphic Strings 的java实现
- MFC——ComBox用法大全
- 阿里巴巴Dubbo实现的源码分析
- c++学习(1)--explicit
- tar 基本操作
- Count and Say
- JVM -XX: 参数介绍
- 对人脸检测训练样本如何生成正样本描述文件和负样本集合文件
- FileStream类
- BZOJ 1116: [POI2008]CLO
- 关于Android中的乱码
- 黄斤德:金价千二上方如履薄冰,耶伦携重磅数据登场
- 设计模式—单例模式分析
- scanf,sscanf高级用法