网址

题目

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

相关文章:

  • 2021-11-25
  • 2021-07-22
  • 2021-09-25
  • 2022-02-28
  • 2021-10-30
  • 2021-06-02
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2022-02-07
  • 2021-07-05
  • 2021-11-30
  • 2021-08-05
  • 2021-11-20
  • 2021-08-11
相关资源
相似解决方案