Easy-readable 6ms recursive code


  • 1
    X

    The current result are always read and count the previous result, so...


        string res, tmp;
    	if (n == 1)  return "1";
    	while (n>0){
    		int count = 1;
    		res = countAndSay(--n);
    		tmp = "";
    		for (int i = 0; i<res.size(); i++){
    			if (res[i] == res[i + 1])	count++;
    			else{
    				tmp += to_string(count) + res[i];
    				count = 1;
    			}
    		}
    		return tmp;
    	}
    }

  • 0
    Y

    i like the neat code..............................


  • 0

    Could you please explain what does --n in countAndSay(--n) mean ?


Log in to reply
 

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