leetcode No38. Count and Say
来源:互联网 发布:军团要塞2 知乎 编辑:程序博客网 时间:2024/06/06 07:07
Question:
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.
给数字编码,1代表1,2代表11,3代表21,4代表1211即n对应的字符串是n-1对应的字符串编码
Algorithm:
迭代即可
Accepted Code:
class Solution {public: string work(string s) { int k=0; string t; while(k<s.size()) { int tmp=s[k]; int count=0; while(s[k]==tmp) { k++; count++; } t.push_back(count+'0'); t.push_back(tmp); } return t; } string countAndSay(int n) { if(n==1)return "1"; string tmp="1"; while(--n) { tmp=work(tmp); //迭代 } return tmp; }};
0 0
- leetcode No38. Count and Say
- leetcode No38. 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
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- ParseResult Exception:java.lang.IllegalStateException: Cannot add header view to list -- setAdapter
- js 函数报错“Uncaught: TypeError xxx is not a function”
- AOP切面编程applicationContext.xml的配置
- jquery插件封装:消息对话框
- RxJava学习笔记1--RxJava原理和简单实现
- leetcode No38. Count and Say
- 学习网站
- 在Linux下面实现QDbus通信机制服务对象注册失败
- 卷1 入门CString与字符编码
- python中的reduce函数
- PHP代码中文输出乱码和转码问题
- AOP切面编程 的切面类
- jquery中attr和prop的区别
- 使用Jquery的Ajax异步提交有二进制数据的表单实现方案