Count and Say

原创 2015年07月08日 22:06:39

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.


Solution:

class Solution {
public:
    string countAndSay(int n) {
        if(n == 0) return "";
        string current = "1";
        string next = "";
        stringstream stream;
        while(--n)
        {
            int count = 1;
            current += '-';
            for(int i = 1; i < current.length(); ++i)
            {
                if(current[i] == current[i-1]) count++;
                else
                {
                    string tmp;
                    stream << count;
                    stream >> tmp;
                    next += tmp + current[i-1];
                    stream.clear();
                    count = 1;
                }
            }
            current = next;
            next.clear();
        }

        return current;
    }
};


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

相关文章推荐

《leetCode》:Count and Say

题目The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 1112...

LeetCode Count and say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11122...

【leetcode c++】38 Count and Say

Count and Say The count-and-say sequence is the sequenceof integers beginning as follows: 1, 11, 2...
  • hqq39
  • hqq39
  • 2015-06-26 22:40
  • 309

[LeetCode][Java] Count and Say

题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 12...

Leetcode - 38. Count and Say

38. Count and Say题目简介 The count-and-say sequence is the sequence of integers beginning as follows:...

leetcode 38:count and say

问题描述:The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...

leetcode 题解 || Count and Say 问题

problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...

LeetCode Count and Say

题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 12...

LeetCode算法题之Count and Say

//问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 121...

<leetcode系列> Count And Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)