前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【 关关的刷题日记47】Leetcode 38. Count and Say

【 关关的刷题日记47】Leetcode 38. Count and Say

作者头像
WZEARW
发布2018-04-11 14:25:33
5800
发布2018-04-11 14:25:33
举报
文章被收录于专栏:专知专知专知

关关的刷题日记47 – Leetcode 38. Count and Say

题目

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

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1 Output: "1" Example 2:

Input: 4 Output: "1211"

题目的意思:将由数字构成的字符串读出来成为一个新的字符串。比如1口语是1个1,记作11;11读作2个1,记作21;21读作1个2,1个1,记作1211……。输入n,计算第n个这样的数字。

思路

思路:按照将字符串读出来成为一个新的字符串的思路来暴力求解。

class Solution {
public:
    string countAndSay(int n) {
        if(n==0)
            return "";
        if(n==1)
            return "1";
        string s="1";
        for(int i=1; i<n; i++)
        {
            int count=1;
            string temp="";
            for(int i=0; i< s.size()-1; i++)
            {
                if(s[i+1]==s[i])
                    ++count;
                else
                {
                    temp=temp+char(count+'0')+s[i];
                    count=1;
                }  
            }
            s=temp+char(count+'0')+s[s.size()-1];
        }
        return s;
    }
};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 专知 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档