LeetCode 题解(230) : Count and Say

本文介绍了一种生成计数读数序列的方法,通过给定一个整数n,可以生成第n个计数读数序列。该序列从1开始,后续每一项都是前一项的计数读数表示。

题目:

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 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, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

题解:

count occurrence of each type of character and make a new string: count1type1count2type2.....

C++版:

class Solution {
public:
    string countAndSay(int n) {
        if(n == 1)
            return "1";
            
        int i = 1;
        string start = "1";
        while(i < n) {
            string end = "";
            string result = "";
            int j = 0;
            while(j < start.length()) {
                char c = start[j];
                end += c;
                j += 1;
                int count = 1;
                while(j < start.length() && start[j] == c) {
                    count++;
                    j++;
                }
                end = to_string(count) + end;
                result += end;
                end = "";
            }
            start = result;
            i++;
        }
        return start;
    }
};

Java版:

public class Solution {
    public String countAndSay(int n) {
        if(n == 1)
            return "1";
        StringBuffer start = new StringBuffer();
        StringBuffer end = new StringBuffer();
        StringBuffer result = new StringBuffer();
        start.append("1");
        int i = 1;
        while(i < n) {
            end.delete(0, end.length());
            result.delete(0, result.length());
            int j = 0;
            while(j < start.length()) {
                char c = start.charAt(j);
                end.append(c);
                int count = 1;
                j++;
                while(j < start.length() && start.charAt(j) == c) {
                    count++;
                    j++;
                }
                end.insert(0, Integer.toString(count));
                result.append(end.toString());
                end.delete(0, end.length());
            }
            start.delete(0, start.length());
            start.append(result.toString());
            i++;
        }
        return start.toString();
    }
}

Python版:

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        if n == 1:
            return "1"
        i = 1
        start = "1"

        while i < n:
            j = 0
            end = ""
            result = ""
            while j < len(start):
                cur = start[j]
                end += cur
                count = 1
                j += 1
                while j < len(start) and start[j] == cur:
                    count += 1
                    j += 1
                end = str(count) + end
                result += end
                end = ""
            start = result
            i += 1
        return start


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值