38. Count and Say
来源:互联网 发布:mips linux gnu gcc 编辑:程序博客网 时间:2024/06/07 07:00
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 12. 113. 214. 12115. 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 term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1Output: "1"
Example 2:
Input: 4Output: "1211"
第n次的字符串是读出第n-1次的字符串,要求求出第n次字符串。没发现第n次的字符串和n之间有关系,所以直接用暴力的方法----从1~n都读一遍。
代码:
class Solution {public:string countAndSay(int n){string s = "1";while(--n){int i = 0;string tmp;s += '#';while(i < s.size()-1){int count = 1;while(s[i] == s[i+1]) {++count;++i;}tmp += to_string(count) + s[i];++i;}s = tmp;}return s;}};
阅读全文
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
- 返回顶部
- secureCRT 日志保存并记录每条记录的时间
- java面试题之JDK和JRE的区别是什么?
- UnicodeWarning
- 33 款主宰 2017 iOS 开发的开源库
- 38. Count and Say
- 离散数学 p313 所有顶点都是偶度数的连通图有欧拉回路 证明
- ie8及其以下版本兼容性问题之文本省略
- Django 自定义认证系统
- 64位win7使用debug的方法
- 智能充电桩——为电动汽车的最后一米铺路!
- [leetcode]41. First Missing Positive(Java)
- 写两个线程,其中一个线程打印1-52,另一个打印A-Z,打印顺序为12A34B56C....5152Z
- 一道常被人轻视的前端JS面试题