美文网首页
LeetCode 第 38 题:外观数列

LeetCode 第 38 题:外观数列

作者: 放开那个BUG | 来源:发表于2024-03-17 17:16 被阅读0次

1、前言

题目描述

2、思路

就是按照题目的步骤,从最小的单元开始,一步步增大

3、代码

class Solution {
    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        String num = countAndSay(n - 1);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for (int i = 0; i < num.length(); i++) {
            if(i + 1 < num.length() && num.charAt(i) == num.charAt(i + 1)){
                count++;
                continue;
            }
            sb.append(count + 1).append(num.charAt(i));
            count = 0;
        }

        return sb.toString();
    }
}

相关文章

  • 外观数列

    LeetCode第38题 题目描述:「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述...

  • LeetCode:38. 外观数列简单求解

    题目:38. 外观数列[https://leetcode-cn.com/problems/count-and-sa...

  • LeetCode - #38 外观数列

    前言 我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤...

  • 38. 外观数列

    38. 外观数列(难度:简单) 题目链接:https://leetcode-cn.com/problems/cou...

  • 数据结构算法基础

    算法基础 一、基础算法 字符串处理 LeetCode38 外观数列 LeetCode49 字母异位词分组 对字母...

  • LeetCode.38 - 外观数列

    题目 声明 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/...

  • LeetCode --- 38.外观数列

    LeetCode --- 字符串、数组简书专栏:https://www.jianshu.com/nb/417965...

  • Python小白 Leetcode刷题历程 No.36-N

    Python小白 Leetcode刷题历程 No.36-No.40 有效的数独、解数独、外观数列、组合...

  • 字符串 - 外观数列

    38. 外观数列 题目描述 给定一个正整数 n(1 ≤ n ≤ 30),输出外观数列的第 n 项。注意:整数序列中...

  • leetcode题目38. 外观数列(java)

    题目描述 给定一个正整数 n(1 ≤ n ≤ 30),输出外观数列的第 n 项。注意:整数序列中的每一项将表示为一...

网友评论

      本文标题:LeetCode 第 38 题:外观数列

      本文链接:https://www.haomeiwen.com/subject/nlfmkdtx.html