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. tcl之list操作-llength/lindex/lrange/linsert/lreplace

  2. Gym 100829S_surf 动态规划的优化

    题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...

  3. 部署 Windows PowerShell Web 访问

    部署 Windows PowerShell Web 访问 适用对象:Windows Server 2012, Windows Server 2012 R2 Windows PowerShell® We ...

  4. Python框架之Django学习笔记(十七)

    Django框架之表单(续二) 今天的这篇博客将是Django学习笔记博客的最后一篇,基本每周最少一篇的Django框架学习,坚持到今天也实属不易,当然了,这个框架的学习仅仅是Django框架的基础部 ...

  5. 63、加速android应用(转载)

    本文转自 http://www.devtf.cn/?p=1097 原文链接 : Speed up your app原文作者 : UDI COHEN译文出自 : 开发技术前线 www.devtf.cn. ...

  6. 【Swap Nodes in Pairs】cpp

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  7. JS 关于 URL 的编码或解码方法

    URL的合法字符 URL的合法字符表示再浏览器的地址栏中不会被转义的字符,有两种: URL元字符:分号(;),逗号(’,’),斜杠(/),问号(?),冒号(:),at(@),&,等号(=),加 ...

  8. Leetcode 600.不包含连续1的非负整数

    不包含连续1的非负整数 给定一个正整数 n,找出小于或等于 n 的非负整数中,其二进制表示不包含 连续的1 的个数. 示例 1: 输入: 5 输出: 5 解释: 下面是带有相应二进制表示的非负整数&l ...

  9. Android数据储存之SharedPreferences

    Android中SharedPreferences通常与Editor连用 接口SharedPreferences常用方法: boolean contains(String str):判断SharedP ...

  10. spring IOC注解方式详解

    本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一,概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以 ...