1. 程式人生 > >力扣算法題—038報數

力扣算法題—038報數

算法題 col out turn == void 算法 say 庫函數

 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報數