博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Count and Say
阅读量:4324 次
发布时间:2019-06-06

本文共 1116 字,大约阅读时间需要 3 分钟。

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.

class Solution {public:    string countAndSay(int n) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        string ans,ans1;        ostringstream os;        if (!n) return ans;        if (1 == n){            ans = "1";            return ans;        }                ans1 = countAndSay(n-1);        int counter = 1;        for(int i = 1; i < ans1.size(); i++){            if (ans1[i] == ans1[i-1]){                counter++;            }else{                os << counter << ans1[i-1];                counter = 1;            }        }        os << counter << ans1[ans1.size() -1];        ans = os.str();        //cout << "n: " << n << " sequence:" << ans << endl;          return ans;    }};

 

转载于:https://www.cnblogs.com/kwill/p/3194819.html

你可能感兴趣的文章
15 个有趣的 JavaScript 与 CSS 库
查看>>
实现iOS语言本地化/国际化
查看>>
ASP.NET MVC学习---(二)EF文件结构
查看>>
年会-2014
查看>>
MBTIles实现
查看>>
创建WPF项目
查看>>
电源模块的PCB设计
查看>>
光猫与普通的家用猫
查看>>
Asp.Net 构架(Http Handler 介绍) - Part.2
查看>>
6.11 spring框架
查看>>
Python--eval()函数
查看>>
【转载】Linux下的crontab定时执行任务命令
查看>>
STM32 HAL库的定时器中断回调函数跟串口中断回调函数
查看>>
vs2010找不到ado.net 实体数据模型解决办法
查看>>
(转)深入理解javascript连续赋值表达式
查看>>
用户场景分析
查看>>
MySQL创建数据库及用户
查看>>
Springboot静态页面放在static路径下还是访问不到
查看>>
centos7 重启网卡失败
查看>>
springboot(一)注解
查看>>