码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】38.报数

时间:2018-05-10 00:02:52      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:--   string   pre   leetcode   code   ==   ret   ++   cpp   

class Solution {
public:
    string countAndSay(int n) {
        string str,ans;
        str="1";
        if(n--==1) return str;
        while(n--){
            ans.clear();
            int left=0,right=0;
            while(right<str.size()){
                if(str[right]==str[left]) right++;
                else{
                    ans+=(right-left+‘0‘);
                    ans+=str[left];
                    left=right;
                    right++;
                }
            }
            ans+=(right-left+‘0‘);
            ans+=str[left];
            str=ans;
        }
        
        return ans;
    }
};

  

【LeetCode】38.报数

标签:--   string   pre   leetcode   code   ==   ret   ++   cpp   

原文地址:https://www.cnblogs.com/lettleshel/p/9017257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!