LeetCode之Count and Say

原创 2015年07月07日 16:25:09
/*这是一道模拟题。第n个序列是根据n-1个序列获得的,所以
通过n-1次循环即可获得结果。*/
class Solution {
public:
    string countAndSay(int n) {
        string res("1");
        while(--n) res = getNext(res);
        return res;
    }
    
    string getNext(string &s){
        string res("");
        for(int i = 0; i < s.size();){
            int j = i + 1;
            for(; j < s.size(); ++j){
                if(s[j] != s[i]) break;
            }
            res += num2string(j-i) + s[i];
            i = j;
        }
        return res;
    }
    
    string num2string(int num){
        string res("");
        while(num > 0){
            char c = '0' + num%10;
            res += c;
            num /= 10;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

《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
  • 384

[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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode之Count and Say
举报原因:
原因补充:

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