[leetcode] 38. Count and Say 解题报告
来源:互联网 发布:长城软件怎么样 编辑:程序博客网 时间:2024/05/22 15:23
题目链接:https://leetcode.com/problems/count-and-say/
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.
思路:本题的意思是用一个计数+字符来编码一个数字,接着再用计数+字符编码上一个结果,这样会形成一个序列。最后将结果保存到字符串中。
代码如下:
class Solution {public: string countAndSay(int n) { string str = "1"; while(--n) { string tem; int i = -1, cnt = 0; char ch = str[0]; while(++i < str.size()) { if(str[i] == ch) cnt++; else { tem += to_string(cnt) + ch; cnt = 1; ch = str[i]; } } tem += to_string(cnt) + ch; str = tem; } return str; }};
0 0
- [leetcode] 38. Count and Say 解题报告
- [Leetcode] 38. Count and Say 解题报告
- LeetCode Count and Say 解题报告
- LeetCode解题报告-- Count and Say
- 【LeetCode】Count and Say 解题报告
- Leetcode #38. Count and Say 数数报数 解题报告
- leetcode解题方案--038--count and say
- count and say 解题
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- 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
- debugfs使用软件查看gpio状态
- Android 单个App显示多个任务列表预览视图
- spring中@ResponseBody ajax返回处理信息给前台 中文乱码问题
- OAuth 2.0
- bootstrap-datetimepicker和uploadify应用总结
- [leetcode] 38. Count and Say 解题报告
- delphi7的快捷键
- 图形图像实验-二值分割代码
- 自定义动画,高级动画的实现
- mysql 的 find_in_set函数使用方法
- Android设计模式之单例设计模式的优缺点
- 服务器+客户端软件+java邮件发送jar
- bzoj2749 外星人 数论
- Codevs_P1261 龙王的礼物(DP+背包)