首页 > 其他 > 详细

LeetCode 38: Count and Say

时间:2017-08-23 14:29:16      阅读:229      评论:0      收藏:0      [点我收藏+]
class Solution {
    public String countAndSay(int n) {
        if (n == 0) {
            return "0";
        }
        String result = "1";
        for (int i = 1; i < n; i++) {
            result = generateCount(result);
        }
        return result;
    }
    
    private String generateCount(String current) {
        int count = 1;
        char currentC = current.charAt(0);
        StringBuilder result = new StringBuilder();
        for (int i = 1; i < current.length(); i++) {
            if (currentC != current.charAt(i)) {
                result.append(String.valueOf(count));
                result.append(currentC);
                currentC = current.charAt(i);
                count = 1;
            } else {
                count++;
            }
        }
        result.append(String.valueOf(count));
        result.append(currentC);
        return result.toString();
    }
}

1. Remember to add it again after counting since there are still remaining information in cache.

LeetCode 38: Count and Say

原文:http://www.cnblogs.com/shuashuashua/p/7417790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!