Count and Say
The Count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as "one 1" or 11 .
11is read off as "two 1s" or 21 .
21is read off "one 2 as, then one 1" or 1211 .
Given an integer n, generate the nth sequence.
Note:the sequence of integers would be represented as a string.
The new function is intended to calculate the number of 1 and 2 per previous.
1 classSolution {2 Public:3 stringUnguarded_convert (Const string&say)4 {5 StringStream SS;6 intCount =0;7 CharLast = say[0];8 9 for(size_t i =0; I <= say.size (); ++i)Ten { One if(Say[i] = =Last ) A { -++count; - } the Else - { -SS << Count <<Last ; -Count =1; +Last =Say[i]; - } + } A at returnss.str (); - } - - stringCountandsay (intN) - { - if(N <=0)return string(); in - stringSay ="1"; to + for(inti =1; I < n; ++i) - { theSay =Unguarded_convert (say); * } $ Panax Notoginseng returnsay; - } the};
Leetcode (38)