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

1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.

Given an integer n, generate the n th sequence.

Note: The sequence of integers will be represented as a string.

题意:返回第n个序列,第i+1个字符串是第i个字符串的读法。参考:GrandyangJustDoIT的博客。

思路:算法就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。代码需要两个循环,第一个是为找到第n个,第二是为了,根据上一字符串的信息来实现当前的字符串。

 class Solution {
public:
string countAndSay(int n)
{
if(n<) return NULL; string res="";
for(int i=;i<n;++i)
{
string temp; //当前序列
res.push_back('*');
int count=; //重复的个数
for(int j=;j<res.size();++j)
{
if(j==)
count++;
else
{
if(res[j] !=res[j-])
{
temp.push_back(count+'');
temp.push_back(res[j-]);
count=;
}
else
++count;
}
}
res=temp;
}
return res;
}
};

[Leetcode] count and say 计数和说的更多相关文章

  1. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

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

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

  3. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  4. [LeetCode]Count and Say 计数和发言

    Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...

  5. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

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

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

  7. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  8. [LeetCode] 466. Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  9. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

随机推荐

  1. 介绍三种PHP加密解密算法

    PHP加密解密算法 这里主要介绍三种常用的加密解密算法:方法一: /** * @param $string 要加密/解密的字符串 * @param string $operation 类型,ENCOD ...

  2. mysql新增和更新表从已有数据库里面获取的sql语句

    在mysql数据库从已有数据库表插入数据到另一表的sql例子 insert into c(`name`) select name from b; 在mysql数据库从已有数据库表更新数据到另一表的sq ...

  3. 学习新框架laravel 5.6 (第二天)-DB,控制器及模型使用

    DB类使用,控制器使用及模型使用 链接数据库: /config/database.php /.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=330 ...

  4. 字典树(Trie)的学习笔记

    按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...

  5. 一起来学习Shell脚本

    Shell脚本 Shell脚本(shell script),是一种为shell编写的脚本程序. 大家所说的shell通常都是指的shell脚本,但其实shell与shell脚本是两个不同的概念.由于习 ...

  6. python2.7入门---元组

        这次我们来学习下python中的元组.首先,基础认知点是,Python的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素, ...

  7. P1794 装备运输_NOI导刊2010提高(04)

    P1794 装备运输_NOI导刊2010提高(04) 题目描述 德国放松对英国的进攻后,把矛头指向了东边——苏联.1943年初,东线的战斗进行到白热化阶段.据可靠情报,90余万德国军队在库尔斯克准备发 ...

  8. 【数据结构】 Queue 的简单实现

    [数据结构] Queue 的简单实现 public class XQueue<T> { /// <summary> /// 第一个元素 /// </summary> ...

  9. 【数据库】 SQL 使用注意点

    [数据库] SQL 使用注意点 一. 索引 1. 常用的搜索条件,都建议加上索引,但状态列除外(该列只有0,1或几个值,不需要加索引,因为没效果) 2. 查询时, 索引列不能做函数处理,会不走索引 3 ...

  10. VectorDrawable在Android中的配置

    一.让Android支持VectorDrawable apply plugin: 'com.android.application' android { defaultConfig { vectorD ...