LeetCode38——Count and Say
来源:互联网 发布:智能家庭解决方案知乎 编辑:程序博客网 时间:2024/06/06 02:32
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.
难度系数:
容易
实现
string countAndSay(int n) { if (n == 1) { return string("1"); } string prev = countAndSay(n-1); int before = 0; int count = 0; string curr; for (int i = 0; i < prev.length(); ++i) { if (prev[i]-'0' == before) { count++; continue; } else if (before != 0) { curr.append(1, '0' + count); curr.append(1, '0' + before); } before = prev[i]-'0'; count = 1; } if (before != 0 && count != 0) { curr.append(1, '0' + count); curr.append(1, '0' + before); } return curr;}
0 0
- LeetCode38——Count and Say
- LeetCode38——Count and Say
- [LeetCode38]Count and Say
- Leetcode38 Count and Say
- leetcode38 Count and Say
- leetcode38---Count and Say
- leetcode38. Count and Say
- leetcode38: Count and Say
- Leetcode38: Count and Say
- LeetCode38. Count and Say
- leetcode38. Count and Say
- 【LeetCode38】【Count and Say】StringBuilder
- LeetCode38 Count and Say(C语言解决)
- leetcode ——Count and Say
- LeetCode——Count and Say
- LeetCode——Count and Say
- leetcode记录——Count and Say
- leetcode 038 —— Count and Say
- How to replace a character for a newline in Vim?
- [前端] 文字垂直滚动+鼠标悬停效果
- [2015.2.11] 操作文件和目录
- UITextView的用法
- 利用GoogleEarth影像制作Skyline MPT案例
- LeetCode38——Count and Say
- IOS之Storyboard导航大揭秘
- CentOS安装scp命令
- ibatis-TypeHandlerCallback使用
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- 【转】单元测试利器 JUnit 4
- 最近才知道的一款软件,可以实现在windows下使用linux下的命令,甚至可以使用简单的shell
- 蓝桥杯——算法提高 日期计算
- TorCMS,基于Tornado的开源内容管理系统