【leetcode】38. Count and Say

网址

题目

【leetcode】38. Count and Say
题意有点难理解,实际上就是一个递归的思路,后一个值是前一个值的叫法。理解题意之后,这题就很简单了。

解法

class Solution {
    public String countAndSay(int n) {
        String seq = "1";
        for(int i = 1; i < n; i++){
            List li = new ArrayList();
            for(int j = 0; j < seq.length(); j++){
                char pre = seq.charAt(j);
                int count = 1;
                while(j+1 < seq.length() && seq.charAt(j+1) == pre){
                    count++;
                    j++;
                }
                li.add(count+""+pre);
            }
            seq = String.join("",li);
        }
        return seq;
    }
}