-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38.count-and-say.cpp
More file actions
48 lines (39 loc) · 1.01 KB
/
38.count-and-say.cpp
File metadata and controls
48 lines (39 loc) · 1.01 KB
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
#include "testharness.h"
#include <string>
#include <string.h>
#include <vector>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
string result;
for (int i = 0; i < n; i++) {
result = getNextString(result);
}
return result;
}
private:
string getNextString(const string& str) {
size_t strLen = str.length();
if (strLen <= 0) return "1";
char* buf = new char[2*strLen];
char* ptr = buf;
size_t i = 0;
while (i < strLen) {
size_t base = i;
char c = str[base];
i = strLen; // we want to break while loop
for (size_t j = base + 1; j <= strLen; j++) {
if (str[j] != c) {
ptr += sprintf(ptr ,"%ld%c", j - base, c);
i = j; // continue loop
break;
}
}
}
return string(buf);
}
};
TEST(Solution, test) {
ASSERT_EQ(2, 1+1);
}