首页 > 其他 > 详细

Count and Say

时间:2014-03-01 03:59:30      阅读:535      评论:0      收藏:0      [点我收藏+]

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.

 

注意char * 和 string的区别。string是一个和vector类,本身是有大小的。如果只

1
2
3
string str;
str[0] = ‘a‘;
str[1] = ‘b‘;

是无法正常改变str的。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
    string countAndSay(int n) {
        string re;
        vector <int> se;
        se.push_back(1);
        if(n == 1)return "1";
         
        for(int i = 0 ; i < n-1 ;i++)se = nth(se);
         
        int j = 0;
        for(int i = 0 ; se.begin() + i != se.end() ; i++)
        {
            char temp = se[i] + ‘0‘;
            re = re + temp;
            j++;
        }
        return re;
    }
    vector<int> nth(vector<int> n )
    {
        vector<int>:: iterator  it;
        vector<int> re;
        it = n.begin();
        for(;it != n.end();)
        {
            int temp = *it;
            //cout<<temp<<endl;
            int num =1;
            it++;
            while(*it == temp && it!= n.end()){it++; num++;}
             
            re.push_back(num);
            re.push_back(temp);
        }
        return re;
    }
};

  

Count and Say,布布扣,bubuko.com

Count and Say

原文:http://www.cnblogs.com/pengyu2003/p/3573876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!