Share my solution beats 94% solutions C++


  • 0
    M
    class Solution {
    public:
        string countAndSay(int n) {
            string res = "";
        	string temp;
        	if (n == 1)
        		return "1";
        	res = "1";
        	for (int i = 1; i < n; i++)
        	{
        		char counter = res[0];
        		int c = 0;
        		for (int j = 0; j < res.length(); j++)
        		{
        			if (res[j] == counter)
        				c++;
        			else
        			{
        				temp += c + '0';
        				temp += counter;
        				counter=res[j];
        				c = 1;
        			}
        		}
        		temp += c + '0';
        		temp += counter;
        		counter++;
        		res = temp;
        		temp.clear();
        	}
        	return res;
        }
    };
    

Log in to reply
 

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