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.

Hide Tags

String

 

  这题其实就是字符串操作,题目有点难懂,意思翻译下,有那么一个数列,第一个为1,后面的结果是基于前一个的,规律如上面的描述,那么第二个便是统计第一个的个数,因为第一个只有1,所以第二个是21,然后第三个是统计第二个的,便是12 11,这样连起来为第三个的1211,第四个是基于第3个的,统计后便是 11 12 21,这样第四便是111221,如此下去,返回第n个的结果,使用string 返回。算法中涉及到int 转换成string 。
  我的思路便是按上面那样一个一个算,其实有更好的实现,需要点额外空间,观察规律可知每项是固定的,所以可以将已经知道的记录下来,这样便能够提升时间效率。
 
#include <string>
#include <iostream>
#include <sstream>
using namespace std; class Solution {
public:
string countAndSay(int n) {
if(n<=) return "";
if(n==) return "";
string curStr="",retStr="";
for(int i=;i<n;i++){
int cnt = ;
retStr="";
for(int j=;j<curStr.size();j++){
if(curStr[j-]==curStr[j]) cnt++;
else{
stringstream ss;
ss<<cnt<<curStr[j-];
retStr+=ss.str();
cnt=;
}
}
stringstream ss;
ss<<cnt<<curStr[curStr.size()-];
retStr+=ss.str();
curStr=retStr;
}
return retStr;
}
}; int main()
{
Solution sol;
for(int i=;i<;i++){
string ret=sol.countAndSay(i);
cout<<"i:"<<ret<<endl;
}
return ;
}

[LeetCode] Count and Say 字符串的更多相关文章

  1. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

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

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

  3. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  4. leetcode题解之分解字符串域名

    1.题目描述 A website domain like "discuss.leetcode.com" consists of various subdomains. At the ...

  5. LeetCode.893-特殊相等字符串组(Groups of Special-Equivalent Strings)

    这是悦乐书的第344次更新,第368篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第209题(顺位题号是893). You are given an array A of ...

  6. LeetCode初级算法之字符串:387 字符串中的第一个唯一字符

    字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...

  7. 【LeetCode】839. 相似字符串组 Similar String Groups (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎 ...

  8. [LeetCode]1221. 分割平衡字符串

    在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...

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

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

随机推荐

  1. Django2.1中的分页功能详解

    django的分页功能类将我们常用的多种方法均封装在Paginator类,根据这些方法我们均可深度定制我们的分页功能. 首先来看看[Paginator] 类的构造方法: class Paginator ...

  2. 将Web项目War包部署到Tomcat服务器基本步骤(完整版)

    1. 常识:   1.1 War包 War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码. 当开发人员在自己 ...

  3. 菜鸟学Linux - Hard Link与Symbolic Link

    在学习Hard Link与Symbolic Link之前,需要大概了解一下inode与data block.在Linux的文件系统中,一个文件对应一个inode与若干个data block.inode ...

  4. JDK各版本新特性浅谈

    JDK 5.0 自动拆装箱 枚举 可变参数 泛型 For -each 内省 静态导入 JDK 6.0 console开发控制台程序 轻量级HTTP ServerAPI 支持脚本语言 使用Compile ...

  5. Tensorflow实现Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图)

    Mask R-CNN实例分割通用框架,检测,分割和特征点定位一次搞定(多图)   导语:Mask R-CNN是Faster R-CNN的扩展形式,能够有效地检测图像中的目标,同时还能为每个实例生成一个 ...

  6. leetcode 【 Search in Rotated Sorted Array 】python 实现

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  7. dijkstra 堆优化

    #include <iostream> #include <vector> #include <cstring> #include <queue> us ...

  8. LeetCode668马在棋盘上的概率

    已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有一个 “马”(也译作 “骑士”)位于 (r, c)  ...

  9. VMSAv8-64 translation table format descriptors

    通常情况下,一个 descriptor 可能是以下的几种 entry: 非法或者异常的 entry. Table entry, 指向 next-level translation table. Blo ...

  10. 打开任意位置的webConfig

    请阅读原文:打开任意的配置文件 翻翻ConfigurationManager的签名,有一个方法吸引了我的注意:OpenExeConfiguration(string exePath).看上去我可以把B ...