LeetCode 38: Count and Say

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 38: Count and Say相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode题意分析&解答38. Count and Say

Leetcode38. Count and Say

LeetCode 38: Count and Say

[LeetCode 38] Count and Say

leetcode-38 Count And Say

LeetCode 38. Count and Say