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

[LeetCode]Count and Say

时间:2015-12-01 16:32:13      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        String str = "1";
        for (int i = 1; i < n; i++) {
            str = count(str);
        }
        return str;
    }
    public String count(String s) {
        int p1 = 0;
        int p2 = 0;
        String result = "";
        while (p2 < s.length()) {
            while (p2 + 1 < s.length() && s.charAt(p2 + 1) == s.charAt(p2)) {
                p2 ++;
            }
            result = result + String.valueOf(p2 - p1 + 1) + String.valueOf(s.charAt(p1));
            p1 = p2 + 1;
            p2 ++;
        }
        return result;
    }
}

 

[LeetCode]Count and Say

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5010428.html

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