【LeetCode】38.报数

class Solution {
public:
    string countAndSay(int n) {
        string str,ans;
        str="1";
        if(n--==1) return str;
        while(n--){
            ans.clear();
            int left=0,right=0;
            while(right<str.size()){
                if(str[right]==str[left]) right++;
                else{
                    ans+=(right-left+‘0‘);
                    ans+=str[left];
                    left=right;
                    right++;
                }
            }
            ans+=(right-left+‘0‘);
            ans+=str[left];
            str=ans;
        }

        return ans;
    }
};

  

原文地址:https://www.cnblogs.com/lettleshel/p/9017257.html

时间: 2024-10-10 21:53:50

【LeetCode】38.报数的相关文章

C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章中我们主要科普了刷 LeetCode 对大家的作用,今天咱们就正式进行 LeetCode 算法题分析.很多人都知道计算机中有种思想叫 递归,相应地也出现了很多算法.解决递归问题的要点有如下几个: 找出递归的关系 比如,给个数列 f(n),常见的递归关系是后面的项 f(n+1)与前面几项之间的关系,比

【leetcode算法-简单】38. 报数

[题目描述] 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作  "one 1"  ("一个一") , 即 11.11 被读作 "two 1s" ("两个一"), 即 21.21 被读作 "one 2",  "one 1" ("一个二" ,  "一个一&

? 38. 报数-LeetCode

心得:效果不理想,后期还会改进 1 class Solution { 2 public String countAndSay(int n) { 3 String str="1"; 4 for(int i=2;i<=n;i++) 5 { 6 char[] s=String.valueOf(str).toCharArray(); 7 str=""; 8 int index=1; 9 for(int j=0;j<s.length;j++) 10 { 11 if

leetCode 38. Count and Say 字符串

38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" 

LeetCode(38)题解: Count and Say

https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as

LeetCode 38 Count and Say(字符串规律输出)

题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111221—>312211—>…. 按照上面的规律进行求解出第n个字符串是什么. 规律:相连的数字有多少个然后添加上这个数字 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 按照规律进行求解字符串 */ public class So

leetcode 38

38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" 

Java [leetcode 38]Count and Say

题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211.

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" o