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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值