38. Count and Say

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.

说真的刚开始真不知道这道题要干什么,英语有点差~~~

当明白是什么意思的时候又发现描述起来有点问题,语文有点差~~~

生硬的描述一下吧:1被读作“1个1” 或  11;11被读作“2个1”或21;21被读作“1个2,1个1”或1211;依次这样下去,输出第n个序列。

代码如下:

 class Solution {
public:
string countAndSay(int n) {
string first = "";
for(int i = ; i < n; i++)
{
string ss = "";
int count = ;
for(int j = ; j < first.length(); j++)
{
if(first[j-] == first[j])
{
count ++;
}
else
{
ss = ss + (char)(count + '') + first[j-];
count = ;
}
}
first = ss + (char)(count + '') + first[first.length()-];
}
return first;
}
};

leetcode 38的更多相关文章

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

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

  2. Java实现 LeetCode 38 外观数列

    38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...

  3. LeetCode - 38. Count and Say

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

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

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

  5. LeetCode(38)题解: Count and Say

    https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integ ...

  6. Leetcode 38.报数 By Python

    报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...

  7. [leetcode] 38. 报数(Java)(字符串处理)

    38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...

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

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

  9. leetcode 38 Count and Say ---java

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

随机推荐

  1. Linux Tcpdump 使用举例 ---持续更新

    举例: 保存到文件tcpdump -w xxx.cap(默认抓取eth0的包) 抓eth1的包 tcpdump -i eth1 -w /tmp/xxx.cap 抓到完成的数据包(默认只抓前68字节) ...

  2. Python 向上取整的算法

    一.初衷: 有时候我们分页展示数据的时候,需要计算页数.一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页. 一般的除法只是取整数部分,达不到要求. 二.方 ...

  3. 502 bad gateway 可能的错误原因

    1.PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间 #http代码段中增加 fastcgi_connect_timeout ...

  4. webview--网络超时

    package com.test.js2java; import java.util.Timer; import java.util.TimerTask; import android.app.Act ...

  5. laravel判断HTTP请求是否ajax

    if(Request->ajax()){ echo "AJAX"; }else{ echo '普通请求':}

  6. Centos下yum配置lnmp环境

    首先关闭SELINUX        vi /etc/selinux/config       #SELINUX=enforcing       #注释掉       #SELINUXTYPE=tar ...

  7. Mac 上SVN上传.a文件

    SVN默认是忽略.a文件,所以修改配置文件去掉忽略配置行的 *.a 通过终端打开配置文件: open ~/.subversion/config 把下面两行(也可能是一行)中的注释和*.a去掉, #gl ...

  8. 解决Ubuntu 12.10中ZIP文件名乱码的方法

    转摘源地址:http://blog.csdn.net/jiangxinyu/article/details/8206395 安装(12.04及以上): 代码: sudo apt-get install ...

  9. [Flex] ButtonBar系列——简单布局

    <?xml version="1.0" encoding="utf-8"?> <!--通过layout属性,设置ButtonBar布局--&g ...

  10. spring mvc中的json整合

    spring mvc整合过程中是有版本兼容的问题.具体的哪个版本的springmvc和哪个个版本的json包冲突我也无从考证了.我用的springmvc版本是3.2.1jaskson的版本是 1.1. ...