LeetCode问题38

Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     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.

Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"

尝试探索第n个count and say数的特征,没找见,尴尬。最直观的想法就是一点点从第一个数据往下算过去,这样的时间复杂度肯定是比较大的,但暴力的方法总能解决问题。计算的核心问题是如何根据第i个count and say数c[i]得到第i+1个count and say数c[i+1],最简单的办法就是从头对c[i]的每位数进行计数,如果相同那么count加一,如果不同将这位数及count值推入结果。最后进行循环即可。(或者迭代效果更好?)最终代码如下:
class Solution {
    public String countAndSay(int n)
    {
        String s="1";//初始化
        for(int i=1;i<n;i++)
        {
            s=change(s);//循环下去
        }
        return s;
    }
    static String change(String s)//对于一个输入的序列得到下一个序列
    {
        StringBuilder res=new StringBuilder();
        int length=s.length();
        int count=1;
        char temp1=s.charAt(0);
        for (int i=1;i<length;i++)
        {
            if(s.charAt(i)==temp1)//判断相同计数
                count++;
            else//判断不同,缓存
            {
                res.append(count);
                res.append(temp1);
                temp1=s.charAt(i);
                count=1;
            }
        }
        res.append(count);//当长度为1和最后一位的数据存入缓存
        res.append(temp1);
        return res.toString();
    }
}
最后消耗时间只超过38%的提交者,表示这个算法意料之内远远没有达到最优。
时间: 2024-10-11 00:32:11

LeetCode问题38的相关文章

[LeetCode] Combinations [38]

题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 原题链接(点我) 解题思路 组合问题,老思路---递归加循环,这个是组合里面比较简单的. 代码实现 class Solutio

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 1

【Leetcode】38. Count and Say

Question: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 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

leetcode第38题--Combination Sum

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

leetcode 第38题

这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个1     2个1     1个2,1个1   1个1,1个2,2个1  3个1,2个2,1个1 依次类推 题目很简单,但是为了得到较好的结果,还是纠结了一段时间 public class countAndSay { public String countAndSay(int n) { String num

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. Given

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. Given

笔试题51. LeetCode OJ (38)

这个题是个数字规律题,题目的意思是,给定一个值 n,按照规律列出第 n 个数字,规律如上图中所示,大概的意思是,表示左边的数字的下一个数字,例如: 1.    "1",它由 1 个 1 表示,则变成"11" 2.    "11",它由2个1组成,则变成"21" 3.      "21",它由1个2和1个1组成,则变成"1211" 规律就是上面这样了,还是比较容易理解.代码如下: clas

【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+=st