LeetCode 038 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" or 1211.

Given an integer n, generate the nth sequence.

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

分析:

看了半天才看懂这道题……

代码如下:

class Solution {
public:
    string countAndSay(int n) {

        string s("1");

        while(--n)
            s = getNext(s);

        return s;
    }

    string getNext(const string &s){
        stringstream ss;

        for(auto i = s.begin(); i != s.end(); ){
            auto j = find_if(i, s.end(), bind1st(not_equal_to<char>(), *i));

            //几个i,核心
            ss << distance(i, j) << *i;
            i = j;
        }

        return ss.str();
    }
};
时间: 2024-08-05 11:17:37

LeetCode 038 Count and Say的相关文章

[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

Java for LeetCode 038 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: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][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