Very easy understand solution C++


  • 1
    X
    class Solution {
    public:
    	string countAndSay(int n) {
    		string str = "1";
    		int count = 1;
    
    		while(count < n) {
    			str = convertToCountAndSay(str);
    			count++;
    		}
    		return str;
    	}
    	
    	string convertToCountAndSay(string str) {
    		int p = 0;
    		string res;
    		while(p < str.length()) {
    			char cur_num = str[p];
    			int count = 0;
    			while(p < str.length() && str[p] == cur_num) { count++; p++; }
    			res += count + '0';
    			res += cur_num;
    		}
    		return res;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.