38. Count and Say
来源:互联网 发布:猪八戒的前世今生 知乎 编辑:程序博客网 时间:2024/05/29 15:40
class Solution {public: string countAndSay(int n) { if( n <= 0){ return NULL; } string dp[n]; dp[0] = "1"; string now; string newstr; for(int i = 1 ; i < n ; i++){ int count = 1; now = dp[i-1]; newstr = ""; int len = now.size(); for(int j = 1 ; j < len ; j++){ //如果和下一个字符一样,就count继续自增 if(now[j-1] == now[j]){ count++; continue; } //如果不一样了,就把在当前数字之前的数字个数加入结果中并且把数字也放入,最后count重新计数 else{ newstr.push_back('0' + count); newstr.push_back(now[j-1]); count = 1; } } //处理源字符串的最后一个字符 newstr.push_back('0' + count); newstr.push_back(now[len -1]); dp[i] = newstr; } return dp[n-1]; }};
有几个点注意:
1.就是string的加入后需字符用的是push_back。
2.对于数字转化字符处理:‘0’ + count。
3.处理好中间结果再加入最后的序列dp中去。
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- Linux虚拟地址空间布局
- git fatal: refusing to merge unrelated histories
- android 广播详解与实例(Broad Receiver)
- npm 常用命令详解
- 标准IO流cin与cout
- 38. Count and Say
- 常见开源加密算法库
- docker命令
- Android反编译
- 股票数据的获取分析
- NOIP2007提高组——矩阵取数游戏(game)
- datagrid选中行
- 【js】日期字符串比较大小
- 仿蘑菇街,蜜芽宝贝,京东商品详情界面,与NestedScroll滑动