The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"
 class Solution {
public:
string countS(string s) {
int l = s.length();
char curChar = s[];
string retStr = "";
int curN = ;
for (int i = ; i < l; i++) {
if (curChar == s[i]) {
curN += ;
if (i == l - ) {
char temp = curN + '';
retStr = retStr + temp + curChar;
}
}
else {
char temp = curN + '';
retStr = retStr + temp + curChar;
curChar = s[i];
curN = ;
if (i == l - ) {
retStr = retStr + '' + curChar;
}
}
}
return retStr;
} string countAndSay(int n) {
if (n == ) {
return "";
}
else {
string s = "";
for (int i = ; i <= n; i++) {
s = countS(s);
}
return s;
}
}
};

LeetCode - 38. Count and Say(36ms)的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  3. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  4. Java [leetcode 38]Count and Say

    题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...

  5. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  6. [LeetCode] 38. Count and Say_Easy

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  7. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  8. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  9. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

随机推荐

  1. 时空隧道FQ

    给你推荐一款海外网站加速工具,为科技工作者.海外归国人员.企业团队.外贸工作者提供海外上网服务,永久免费. 国外网址:https://chrome.google.com/webstore/detail ...

  2. Centos安装VMware

    转载:http://www.mamicode.com/info-detail-2171464.html

  3. div鼠标悬停,子元素上移,鼠标移出,子元素下移动画。

    HTML: <div class="edt_title" > <div id="edt_title"> <p class=&quo ...

  4. update、commit、trancate,delete

    update 用于更新表的数据,使用方式为: update table_name set column_name=值 条件 顺便一提:date数据插入更新应该使用 to_date()格式转换函数例如: ...

  5. UIImagePickerController获取照片的实现,添加overlay方法 (相机取景框)

    DEVELOPER.XIAOYAOLI 技术笔记 简单的利用UIImagePickerController调用iPhone摄像头获取照片的方法,同时介绍了怎么添加overlay,用于自定义预览界面   ...

  6. Vue--- vue-Router 路由

    文档 https://router.vuejs.org/zh-cn npm  install vue-router --save 调用 import Vue from 'vue' import Vue ...

  7. thinkphp模板如何转换时间格式?

    <!-- 如果有日期输出,即$data.time不为空且不为0,则格式化时间戳,否则默认当前时间戳,并格式化成日期格式 --> {$data.time|default=time()|dat ...

  8. Ubuntu 配置多域名站点

    思路 -- 跟Windows 一样 1添加Nginx 指向项目的入口 配置域名 2修改本地host文件域名指向 实现: 1 进入Nginx 配置文件  默认地址为 /etc/nginx/sites-e ...

  9. ExceL按记录导出Txt 工具

    根据客户要求,开发此工具,每一条记录改出一个Txt文本,文本名取其中一字段数据

  10. scala成长之路(2)对象和类

    scala提供了一种特殊的定义单例的方法:object关键字 scala> object Shabi{ | val age = 0 | val name = "shabi" ...