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

LeetCode 38: Count and Say

时间:2017-08-23 14:52:16      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:log   generate   ber   valueof   else   build   leetcode   member   tin   

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

标签:log   generate   ber   valueof   else   build   leetcode   member   tin   

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

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