LeedCode-Easy 38. Count and Say

题目原文

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"
Explanation: This is the base case.

Example 2:

Input: 4
Output: "1211"
Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12" which means frequency = 1 and value = 2, the same way "1" is read as "11", so the answer is the concatenation of "12" and "11" which is "1211".

翻译一下大意:

吐槽,这题我看了蛮久的

这个解释是用的

大佬的地址

题目解释:原题的意思就是用一个新的字符串描述上一个字符串,用数字表示上一个:
当n=1时:输出1;
当n=2时,解释1,1读作1个 ,表示为11;
当n=3时,解释上一个11,读作2个1,表示为21;(注意相同数字的描述)
当n=4时,解释上一个21,读作1个2,一个1,表示为1211;
当n=5时,解释上一个1211,读作1个1,1个2,2个1,表示为111221;
当n=6时,解释上一个111221,读作3个1,2个2,1个1,表示为312211;

柳神的代码

地址

class Solution {
public:
    string countAndSay(int n) {
        string temp = "";       //用来存放最后的结果
        string num = "";
        int cnt = 1;
        if(n == 1) return "1";
        if(n == 2) return "11";
        string a = "11";
        for(int i = 1; i <= n - 2; i++) {
            for(int j = 1; j < a.length(); j++) {
                if(a[j-1] == a[j]) cnt++;
                if(a[j-1] != a[j]) {
                    while(cnt) {
                        num = (char)(cnt % 10 + '0') + num;
                        cnt = cnt / 10;
                    }
                    temp += num;
                    num = "";
                    temp += a[j-1];
                    cnt = 1;
                }
                if(j == a.length() - 1) {   //到了最后一位了
                    while(cnt) {
                        num = (char)(cnt % 10 + '0') + num;
                        cnt = cnt / 10;
                    }
                    temp += num;
                    num = "";
                    temp += a[j];
                    cnt = 1;
                }
            }
            a = temp;
            temp = "";
        }
        return a;
    }
};

网上的大佬

地址

string countAndSay(int n)
{
    string curr_str;
	// The initial case, when n = 1
	curr_str += '1';
	// The iterative case, when n > 1
	for (int i = 0; i < n - 1; i++)
	{
		string buffer;
		// Handle the current string
		int index = 0;
		for (int index = 0; index < curr_str.size(); ++index)
		{
			// Count the occurance of each digit
			int cnt = 1; // At least one occurance
			while (index + 1 < curr_str.size() and curr_str[index + 1] == curr_str[index]) 
			{
				index++;
				cnt++;
			}
			buffer.push_back(cnt + '0');
			buffer.push_back(curr_str[index]);
		}
		// Update the current string
		curr_str = buffer;
	}

	return curr_str;
}

猜你喜欢

转载自blog.csdn.net/Huangpengyu123/article/details/107093576