LeetCode: Count and Say [037]

【题目】

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.

【题意】

有一个这样的序列,序列中的每个数都是从前一个数通过count-and-say规则生成。

所谓count-and-say就是数数然后读出来,按照读法来组织新的数。举个例子来说

1     读成“1个1” ==>  11

11    读成“2个1” ==> 21

21    读成“1个2,1个1” ==> 1211

目标是返回序列中的第n个数

【思路】

按规则顺序生成序列中的前n个数

【代码】

class Solution {
public:
    string getNext(string integer){
        int count=0;
        char digit=‘\0‘;
        string newInteger="";
        for(int i=0; i<integer.length(); i++){
            if(integer[i]!=digit){
                if(digit!=‘\0‘){
                    char countChar=‘0‘+count;
                    newInteger+=countChar;
                    newInteger+=digit;
                }
                count=1;
                digit=integer[i];
            }
            else{
                count++;
            }
        }
        if(digit!=‘\0‘){
            char countChar=‘0‘+count;
            newInteger+=countChar;
            newInteger+=digit;
        }
        return newInteger;
    }
    string countAndSay(int n) {
        int count=1;
        string integer="1";
        if(n<1)return "";   //考虑一些非法值,题目没有规定,那就设定为空
        if(n==1)return integer;

        while(count<n){
            integer=getNext(integer);
            count++;
        }
        return integer;
    }
};

LeetCode: Count and Say [037],布布扣,bubuko.com

时间: 2024-12-20 16:34:51

LeetCode: Count and Say [037]的相关文章

LeetCode: 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

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 retur

[LeetCode] 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 of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description 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]$

LeetCode -- Count Digit One

题目描述: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本题目纯粹是找规律求解. 该实现参考了:

[LeetCode] Count Univalue Subtrees 计数相同值子树的个数

Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST

LeetCode Count and Say 数数字

1 class Solution { 2 public: 3 string countAndSay(int n) { 4 if(n==1) return "1"; 5 string str0="",str1="1"; 6 int i,t,count; 7 char c='*'; 8 for(i=0;i<n-1;i++){ //一共要数n-1次,假如n=2,那么只要数str1这一次就行了 9 count=1; 10 if(i%2!=0){ /