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 integer n, generate the nth sequence.

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

二.题目分析

题目的内容很多,其实就是根据一个数的读法,组合出下一个数,比如11,读作2(个)1,因此下个数是21;同理,21读作1(个)2、1(个)1,因此下个数是1211…

根据题意,发现该题没有什么高级的技巧,代码中主要使用了stringstringstream,需要加入头文件#include <string>#include <sstream>,关于stringstringstream的用法,可参考:http://blog.csdn.net/xw20084898/article/details/21939811 。该程序只是实现了对题目要求的模拟,然后输出结果。

三.示例代码

#include <string>
#include <sstream>
using namespace std;

class Solution
{
public:
    string CountAndSay(int n)
    {
        string result = "1";

        while (--n)
            result = theNextStr(result);
        return result;
    }

private:
    string theNextStr(const string& str)
    {
        if (str.empty())
            return string();
        stringstream result;
        int strSize = str.size();
        int count = 1; // 计数

        for (int Index = 0; Index < strSize - 1; Index++)
        {
            if (str[Index] == str[Index + 1])
                count++;
            else
            {
                result << count << str[Index];
                count = 1;
            }
        }
        result << count << str[strSize - 1]; // 最后一位
        return result.str();
    }
};

四.小结

需要好好学习一下stringstringstream的用法。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 10:31:28

leetcode笔记:Count and Say的相关文章

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

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笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

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笔记:Pascal&amp;#39;s Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

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笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

[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笔记:Count Primes

一. 题目描述 Count the number of prime numbers less than a non-negative number, n. 二. 题目分析 题目有很多tips,大意是算出2 ~ n之间有多少个素数. 若使用暴力法只会是超时,而正确的思路来自著名的埃拉托斯特尼筛法.简单来说,要得到自然数n以内的全部素数,必须把不大于sqrt(n)的所有素数的倍数剔除,剩下的就是素数.更多关于埃拉托斯特尼筛法,参照: http://baike.baidu.com/link?url=A