38. Count and Say 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 iterative12345678910111213141516171819202122class Solution {public: string countAndSay(int n) { string ret = "1"; for(int i=1;i<n;++i){ string path ; int count = 1, val = ret[0]; for(int j=1;j<ret.size() ; ++j){ if(ret[j] == val) count++; else{ path+=to_string(count); path+=val; count = 1; val = ret[j]; } } if(count > 0) {path+=to_string(count);path+=val;} ret = path; } return ret; }}; option 2 recursive1 analysis time complexity O(nlogn) space complexity O(nm)