首页 > 其他 > 详细

[LeetCode]Count and Say

时间:2015-12-01 16:30:00      阅读:137      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!