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

点击上方“专知”,关注获取专业AI知识”

关关的刷题日记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)。

专知网站查看Leetcode刷题日记:

请登录www.zhuanzhi.ai或者点击阅读原文,顶端搜索“Leetcode” 主题,取查看获得专知Leetcode所有资源!如下图所示~


群满,请扫描小助手(备注leetcode),加入专知-LeetCode学习交流群,交流分享~


欢迎转发到你的微信群和朋友圈,分享专业AI知识


-END-

欢迎使用专知

专知,一个新的认知方式!专注在人工智能领域为AI从业者提供专业可信的知识分发服务, 包括主题定制、主题链路、搜索发现等服务,帮你又好又快找到所需知识。


使用方法>>访问www.zhuanzhi.ai, 或点击文章下方“阅读原文”即可访问专知

中国科学院自动化研究所专知团队

@2017 专知

专 · 知

关注我们的公众号,获取最新关于专知以及人工智能的资讯、技术、算法、深度干货等内容。扫一扫下方关注我们的微信公众号。

点击“阅读原文”,使用专知

展开全文
Top
微信扫码咨询专知VIP会员