Leetcode NO.38 Count and Say
来源:互联网 发布:java老师王克晶年薪 编辑:程序博客网 时间:2024/06/06 08:45
本题题目要求如下:
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.
本题没有什么算法可言,就是对连续出现的同样数字记数。。比如1121,就出现了2个1,1个2,1个1,所以结果是211211,代码如下:class Solution {public: string countAndSay(int n) { string ret = "1"; for (int i = 1; i < n; ++i) { ret = generate(ret); } return ret; }private: string generate(string str) { string ret = ""; char tmp = str[0]; int cnt = 1; for (int i = 1; i < str.length(); ++i) { if (str[i] == tmp) { ++cnt; } else { ret = ret + to_string(cnt) + tmp; cnt = 1; tmp = str[i]; } } ret = ret + to_string(cnt) + tmp; return ret; }};
0 0
- Leetcode NO.38 Count and Say
- LeetCode--No.38--Count and Say
- LeetCode No.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
- dispatchTouch,onInterceptTouch,onTouch
- ios开发证书CSR文件、CER文件、P12文件,mobileprovition许可文件的用途
- 集成Android SlidingMenu(SlideMenu)
- SSH框架总结(框架分析+环境搭建+实例源码下载)
- 取精华、去糟粕!适合iOS开发者的15大网站推荐
- Leetcode NO.38 Count and Say
- 理解Linux系统负荷 查看CPU个数
- 饿了么网站订餐外卖系统功能介绍
- 董老师走进斯坦福讲座
- hdu1238——Substrings——————【求多个字串的最长公共子串】
- C#设计模式学习笔记-单例模式
- EditText使用详解-包含很多教程上看不到的功能演示
- 10个无版权限制的免费图片素材资源网站
- 一些linux系统命令