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中 include 、include_once、require、require_once4个语言结构的含义和区别

    对于不同页面中的相同代码部分,可以将其分离为单个文件 ,通过include引入文件. 可以提高代码的复用率 include 和include_once都有引入文件的作用 使用的语法是 :include ...

  2. phpredis命令

    <?php //redis //检查一个扩展是否已经加载.大小写不敏感. if (!function_exists('redis')) { echo '不支持 redis'; return ; ...

  3. 使用mysql5.7版本数据库需要注意的地方/持续更新

    数据库mysql 5.7版本的初始密码修改 安装完后实在是找不到初始密码的文件,后面发现再错误日志中 先关闭mysql pkill mysqld 安全模式启动数据库并修改密码 mysqld_safe ...

  4. Create Fiori List App Report with ABAP CDS view – PART 2

    In the Part 1 blog, we have discussed below topics CDS annotations for Fiori List Report. How to cre ...

  5. struts2官方 中文教程 系列十三:利用通配符选择方法

    介绍 在本教程中,我们将介绍如何在struts.xml中配置action节点以达到仅使用一个action节点将几个不同的url关联到特定action类的特定方法.这样做的目的是减少struts.xml ...

  6. 【Consul】Consul架构-Consensus协议

    Consul使用Consensus协议提供一致性(Consistency)--CAP定义的一致性.Consensus协议是基于"Raft:In search of an Understand ...

  7. 3155: Preprefix sum

    3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...

  8. Hibernate-ORM:06.Hibernate中三种状态

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客主要叙述Hibernate中的三种状态:临时状态(瞬时状态),持久状态,游离状态 commit和flu ...

  9. cgi、fastcgi、php-cgi、php-fpm的关系

    1. CGI CGI全称是"公共网关接口"(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行"交谈"的一种工具,其 ...

  10. Django笔记 —— 基础部分总结

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...