LeetCode Count and Say Simulation
来源:互联网 发布:pb程序员 编辑:程序博客网 时间:2024/05/20 16:11
思路:
根据第一个推出第二个,第二个推出第三个,以此类推。
时间复杂度为O(N*M),N为轮数,M为每一轮字符串长度。
空间复杂度为O(M)。
class Solution {public: string countAndSay(int n) { if(n == 0) return ""; if(n == 1) return "1"; string base = "1"; string ans; for(int i = 1; i < n; ++i) { int len = static_cast<int>(base.size()); for(int j = 0; j < len; ++j) { int count = 1; while((j+1<len) && (base[j] == base[j+1])) { count++; j++; } ans += to_string(count) + base[j]; } base = ans; ans.clear(); } return base; }};
1 0
- LeetCode Count and Say Simulation
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- 屏蔽OCulus Rift的官方警告的方法实测
- python中内建函数
- JavaScript 作用域和作用域链
- JS地图标注实现
- 引用多个spring-mVC配置文件
- LeetCode Count and Say Simulation
- 【C语言】实现一个简单的通讯录
- 浅谈ES6的 promise API
- iOS开发block(二)
- grab raw keyboard input from event device node
- cc.pool 缓存池
- x264源代码简单分析:滤波(Filter)部分
- 进程和线程概念介绍
- 透过 Linux 内核看无锁编程