Leetcode (3) 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 an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

题目详解

个人认为这是一道非常神奇的题目,因为刚开始做的时候,我竟然完全没有理解题目的意思。刚开始我以为是要给一个字符串,eg. 111221,然后输出其个数的描述,如”three 1, two 2, one 1”或是”312211”这种形式就可以了。后来提交之后说我结果不对,搜了一下才知道原来相当于一个序列的描述。

输入整数n,输入序列中的第n个字符串,字符串的规则如下:

  • n=1, s=”1”
  • n=2, s=”11” (这里的”11”就是对n=1时字符串的描述)
  • n=3, s=”21” (对n=2时字符串的描述)
  • n=4, s= “1211” (对n=3时字符串的描述)

根据上述规则,以此类推,得到第n个字符串的描述。

解决思路

题目的解决思路是需要保存前一个字符串,在对前一个字符串进行统计得到当前字符串的一个循环操作。

字符串统计即通过对字符串进行遍历,若后一个字符串与前一个字符串相等,则对字符串的计数count+1,若不相等则重新设置前一个字符串,并将计数count置为0.

另外不要忘记了对输入值的一个判断,若n<0则返回空字符串。废话不多说,直接上代码。

class Solution {
public:
    string countAndSay(int n) {
        if (n < 0)
            return "";

        string res = "1";
        int i = 1;
        while (i < n)
        {
            string next;
            int j = 0;
            while (j < res.size())
            {
                char priv = res[j];
                int count = 0;
                while (j < res.size() && res[j] == priv)
                {
                    count++;
                    j++;
                }
                next.append(1, count + ‘0‘);
                next.append(1, priv);
            }
            res = next;
            i++;
        }
        return res;
    }
};

上面这段代码的运行时间为3ms,已经算是比较高效的了,在这之前我实现了一个比较慢的算法(18ms),这里也贴出来,供比较,从代码可以看出上一个代码的比较比较简洁,且字符串的增加直接使用了string类型的append函数,而下面的代码比较部分比较繁琐,进行了多次if判断,在得到结果时使用了stringstream类将数字转为字符串,这些都是造成该算法较慢的原因。

class Solution {
public:
    string countAndSay(int n) {
        if (n < 0)
            return "";
        int i = 1;
        string last = "1";

        for (i = 2; i != n + 1; ++i)
        {
            string builder;
            string::iterator it = last.begin();
            int count(0);
            for (it; it != last.end(); ++it)
            {
                count++;
                if ( (it+1)!=last.end() && *it != *(it+1))
                {
                    stringstream ss;
                    ss << count << *it;
                    builder += ss.str();
                    count = 0;
                }
                else if (it+1 == last.end())
                {
                    stringstream ss;
                    ss << count << *it;
                    builder += ss.str();
                }
            }
            last = builder;
        }
        return last;
    }
};
时间: 2024-10-17 20:45:17

Leetcode (3) Count and Say的相关文章

LeetCode:Count and Say

1.题目名称 Count and Say(按照数字重复出现计数并生成字符串) 2.题目地址 https://leetcode.com/problems/count-and-say/ 3.题目内容 英文:The count-and-say sequence is the sequence of integers beginning as follows 中文:给出正整数n,返回"count-and-say"序列的第n项 说明: count-and-say序列形如:1, 11, 21, 1

LeetCode:Count Primes - 统计质数数量

1.题目名称 Count Primes(统计质数数量) 2.题目地址 https://leetcode.com/problems/count-primes/ 3.题目内容 英文:Count the number of prime numbers less than a non-negative number, n. 中文:统计正整数n以内(不含n本身)质数的数量 4.一个TLE的方法 从1到n,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav

Leetcode problem-204 Count Primes 题解

Leetcode problem-204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小于n的数都进行判断是否为素数并计数会超时,因此采用筛法来解这题.建一个数组,从2开始, 把其倍数小于N的都删掉. class Solution { public: int countPrimes(int n) { vector<int>arr(n,1); int s

[LeetCode] 038. Count and Say (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Count and Say (Easy) 链接: 题目:https://leetcode.com/problems/Count-and-Say/ 代码(github):https://github.com/illuz/leetcode 题意: 数数,第一个是 1,第二个是数前一个数:1 个 1,就是 11

[LeetCode][JavaScript]Count of Smaller Numbers After Self

Count of Smaller Numbers After Self You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Given nums = [5,

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/ 题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range

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][JavaScript]Count Primes

Count Prime Description: Count the number of prime numbers less than a non-negative number, n. https://leetcode.com/problems/count-primes/ 找出所有小于n的数中的质数. 删数法.开一个1到n的数组,删除所有2的倍数,3的倍数...直到√n的倍数,最后剩下的就是质数. 1 /** 2 * @param {number} n 3 * @return {number

[LeetCode][JavaScript]Count Complete Tree Nodes

Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last le

LeetCode 204 Count Primes(质数计数)(*)

翻译 计算小于一个非负整数n的质数的个数. 原文 Count the number of prime numbers less than a non-negative number, n. 分析 这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了.题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败-- public int countPrimes(int n) { int count = 0; for (int i = 1; i < n; i++