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. 文件 FIFO队列

    <?php /** * Filefifo.php 文件型FIFO队列 */ class Filefifo { /** * $_file_data, 数据文件的路径 */ private $_fi ...

  2. jquery实现点击页面其他地方隐藏指定元素

    代码实例如下: <!DOCTYPE html><html><head><meta charset=" utf-8"><meta ...

  3. 在java中使用正则表达式注意的地方

    1. 对^与$的理解 通常我们会通过类似Matcher matcher = Pattern.compile(regex).matcher(string);的代码去拿到一个Matcher对象.这种情况下 ...

  4. Informix如何释放异常的锁资源

    问题 在Informix数据库中,锁的使用和释放是自动完成的.但在某些异常情况下,当前台程序退出(正常或异常)后,相应在数据库中的会话没有终止,其占有的资源(主要是锁)没有被释放,影响了其他用户的使用 ...

  5. C++学习44 格式化输出,C++输出格式控制

    在输出数据时,为简便起见,往往不指定输出的格式,由系统根据数据的类型采取默认的格式,但有时希望数据按指定的格式输出,如要求以十六进制或八进制形式输出一个 整数,对输出的小数只保留两位小数等.有两种方法 ...

  6. [ActionScript 3.0] Away3D 非skybox的全景例子

    package { import away3d.containers.View3D; import away3d.controllers.HoverController; import away3d. ...

  7. Enterprise Library 5.0 系列教程

    1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...

  8. C++primer 练习15.26

    定义Quote和Bulk_Quote的拷贝控制成员,令其与合成的版本行为一致.为这些成员以及其他构造函数添加打印状态的 语句,使得我们能够知道正在运行哪个程序.使用这些类编写程序,预测程序将创建和销毁 ...

  9. ThreadLocal意为变量副本

    http://blog.csdn.net/lufeng20/article/details/24314381

  10. C# 泛型List用法

    C# List Examples by Sam Allen - Updated September 6, 2009 Problem. You have questions about the List ...