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. 精致的系统监控工具-netdata

    今天在网上瞎逛,偶然发现一款监控工具:netdata,感到一惊,监控工具竟然可以这么漂亮! 简单了解一下,这款工具还算比较新,监控系统运行状态的功能非常强大,除了监控cpu,网卡,磁盘,内存,进程等等 ...

  2. nginx反向代理后端web服务器记录客户端ip地址

    nginx在做反向代理的时候,后端的nginx web服务器log中记录的地址都是反向代理服务器的地址,无法查看客户端访问的真实ip. 在反向代理服务器的nginx.conf配置文件中进行配置. lo ...

  3. windows 时间同步至最新时间方法 | windows 时间同步服务器

    国内 windows 系统的电脑有时候不能自动同步互联网当前时间,这就需要改一下 windows 的时间同步服务器 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址:https://ww ...

  4. python笔记-dict字典的方法2

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' 概述: 使用键值(key-value)存储,具有极快的查找速度 注意:字典是无序的 key的特性: 1. ...

  5. 批处理文件执行cmd命令

    @echo offstart "wumin" "C:\Windows\System32\cmd.exe" osk taskkill /f /im cmd.exe ...

  6. OpenCV学习笔记(一) 环境配置

    Visual Studio 2010 VS2010对应的OpenCV的lib文件(build\x86\vc10\lib)分为debug模式和release模式两种:debug模式牺牲速度,但能提供更多 ...

  7. 2 Model层 - 模型查询

    1.简介 查询集表示从数据库中获取的对象集合 查询集可以含有零个.一个或多个过滤器 过滤器基于所给的参数限制查询的结果 从Sql的角度,查询集和select语句等价,过滤器像where和limit子句 ...

  8. [译]Exactly once is NOT exactly the same

    近日学习Pulsar文档时,注意到Pulsar提到其提供的是effectively-once语义,而不是其它流计算引擎announce的exactly-once语义,并引用了Exactly once ...

  9. loj6387 「THUPC2018」绿绿与串串 / String

    还是很好做的,大致就是manacher,每个位置为中心的最长回文串要是能抵到最右边就合法,要是能抵到最左边,那这个点的是否合法取决于以这个点为中心的最长回文串的右端点是否合法. #include &l ...

  10. loj2071 「JSOI2016」最佳团体

    分数规划+树形依赖背包orz #include <iostream> #include <cstring> #include <cstdio> #include & ...