leetcode No38. Count and Say

Question:

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.

2读1,3读2,n读n-1,根据上面的规则。

Algorithm:

迭代

Accepted Code:

class Solution {
public:
    string work(string s)
    {
        int k=0;
        string t;
        while(k<s.size())
        {
            int tmp=s[k];
            int count=0;
            while(s[k]==tmp)
            {
                k++;
                count++;
            }
            t.push_back(count+'0');
            t.push_back(tmp);
        }
        return t;
    }
    string countAndSay(int n) {
        if(n==1)return "1";
        string tmp="1";
        while(--n)
        {
            tmp=work(tmp);    //迭代
        }
        return tmp;
    }
};

时间: 2024-09-30 09:21:42

leetcode No38. 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