力扣算法题—038报数

 1 #include "000库函数.h"
 2 
 3 
 4 //自解,就遍历数数  8ms
 5 class Solution {
 6 public:
 7     string countAndSay(int n) {
 8         if (n == 0)return "";
 9         string str = "1";
10         string s;
11         for (int i = 1; i < n; ++i) {
12             s = "";
13             int n = 0;
14             char a = str[0];
15             for (int j = 0; j < str.size(); ++j) {
16                 if (str[j] == a)
17                     ++n;
18                 else {
19                     s += n + '0';
20                     s += a;
21                     a = str[j];
22                     n = 1;
23                 }
24             }
25             s += n + '0';
26             s += a;
27             str = s;
28         }
29         return str;
30     }
31 };
32 
33 void T038() {
34     Solution s;
35     string str;
36     str = s.countAndSay(4);
37     cout << str << endl;
38     str = s.countAndSay(1);
39     cout << str << endl;
40     str = s.countAndSay(5);
41     cout << str << endl;
42 }

 

上一篇:038.[转] JVM启动过程与类加载


下一篇:第 5 章 Nova - 038 - Shelve Instance 操作详解