[LeetCode]Count and Say

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;
    }
}

 

posted @ 2015-12-01 16:11  Weizheng_Love_Coding  阅读(118)  评论(0编辑  收藏  举报