[字符串]leetcode38:报数(easy)

编程入门 行业动态 更新时间:2024-10-24 09:27:21

[<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串]leetcode38:报数(easy)"/>

[字符串]leetcode38:报数(easy)

题目:

38. 报数

题解:

阅读理解题,读懂题目意思后,暴力解题即可。

代码如下:

class Solution {
public:string countAndSay(int n) {string result="1";if(n==1)return result;for(int i=1;i<n;++i){string temp="";char c=result[0];//取第一个字符作为初始判断int count=1;for(int j=1;j<result.size();++j){if(c==result[j])count++;//相等增加该字符的数量else{//不相等则增见数字个数和字母temp.append(to_string(count));temp.push_back(c);//重新设置初始判断的数字,并将数量设为1c=result[j];count=1;}}//将最后的重复字符添加temp.append(to_string(count));temp.push_back(c);result=temp;}return result;}
};

更多推荐

[字符串]leetcode38:报数(easy)

本文发布于:2023-07-28 18:53:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   easy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!