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

38. Count and Say

时间:2016-08-14 16:07:09      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public String countAndSay(int n) {
       
        if(n==1)
            return "1";
        String res="1";
        for(int i=1;i<n;i++)
        {
            res=Say(res);
        }
        return res;
    }
    public String Say(String s)
    {
        int size=s.length();
        int count=0;
        String res="";
        char c=‘0‘;
        for(int i=0;i<size;i++)
        {
            c=s.charAt(i);
            count=1;
            while(i+1<size&&s.charAt(i+1)==s.charAt(i))
            {
                i++;
                count++;
            }
            
            res+=count;
            res+=c;
        }
        return res;
        
    }
}

 

38. Count and Say

标签:

原文地址:http://www.cnblogs.com/aguai1992/p/5770280.html

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