-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountAndSay.h
73 lines (62 loc) · 1.8 KB
/
CountAndSay.h
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Author: naiyong, aonaiyong@gmail.com
Date: Nov 18, 2014
Problem: Count and Say
Difficulty: 2
Source: https://oj.leetcode.com/problems/count-and-say/
Notes:
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: 1. find_first_not_of.
2. iteration.
*/
#ifndef COUNTANDSAY_H_
#define COUNTANDSAY_H_
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
class Solution {
public:
string countAndSay(int n) {
return countAndSayIter(n);
}
// find_first_not_of
string countAndSayStr(int n) {
string s("1");
for (int i = 2; i <= n; ++i) {
stringstream ss;
size_t pos = 0, start = 0;
while ((pos = s.find_first_not_of(s[start], start)) != string::npos) {
ss << pos - start << s[start];
start = pos;
}
ss << s.size() - start << s[start];
ss >> s;
}
return s;
}
// iteration
string countAndSayIter(int n) {
string s("1");
for (int i = 2; i <= n; ++i) {
stringstream ss;
int j = 0, sz = s.size();
while (j < sz) {
int k = j + 1;
while (k < sz && s[k] == s[k-1])
++k;
ss << k - j << s[j];
j = k;
}
ss >> s;
}
return s;
}
};
#endif /* COUNTANDSAY_H_ */