[LeetCode] 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, generate the nth term of the count-and-say sequence.

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

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"

Input Constraints:

1 <= n <= 30

对于前一个数,每一段相同元素的子数列数出相同元素的个数,然后这个子数组变为个数+数字,重复到结束。

解法: 迭代Iteration

Java:

public String countAndSay(int n) {
	if (n <= 0)
		return null;

	String result = "1";
	int i = 1;

	while (i < n) {
		StringBuilder sb = new StringBuilder();
		int count = 1;
		for (int j = 1; j < result.length(); j++) {
			if (result.charAt(j) == result.charAt(j - 1)) {
				count++;
			} else {
				sb.append(count);
				sb.append(result.charAt(j - 1));
				count = 1;
			}
		}

		sb.append(count);
		sb.append(result.charAt(result.length() - 1));
		result = sb.toString();
		i++;
	}

	return result;
} 

Java:

public class Solution {
    public String countAndSay(int n) {
	    	StringBuilder curr=new StringBuilder("1");
	    	StringBuilder prev;
	    	int count;
	    	char say;
	        for (int i=1;i<n;i++){
	        	prev=curr;
	 	        curr=new StringBuilder();
	 	        count=1;
	 	        say=prev.charAt(0);

	 	        for (int j=1,len=prev.length();j<len;j++){
	 	        	if (prev.charAt(j)!=say){
	 	        		curr.append(count).append(say);
	 	        		count=1;
	 	        		say=prev.charAt(j);
	 	        	}
	 	        	else count++;
	 	        }
	 	        curr.append(count).append(say);
	        }
	        return curr.toString();

    }
}

Python:

class Solution:
    # @return a string
    def countAndSay(self, n):
        seq = "1"
        for i in xrange(n - 1):
            seq = self.getNext(seq)
        return seq

    def getNext(self, seq):
        i, next_seq = 0, ""
        while i < len(seq):
            cnt = 1
            while i < len(seq) - 1 and seq[i] == seq[i + 1]:
                cnt += 1
                i += 1
            next_seq += str(cnt) + seq[i]
            i += 1
        return next_seq

Python: wo

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s = ‘1‘
        i = 1
        while i < n:
            count = 1
            curr = s[0]
            news = ‘‘
            for j in xrange(1, len(s)):
                if curr == s[j]:
                    count += 1
                else:
                    news += str(count) + s[j-1]
                    curr = s[j]
                    count = 1
            news += str(count) + s[-1]
            s = news
            i += 1    

        return s    

C++:

class Solution {
public:
    string countAndSay(int n) {
        if (n <= 0) return "";
        string res = "1";
        while (--n) {
            string cur = "";
            for (int i = 0; i < res.size(); ++i) {
                int cnt = 1;
                while (i + 1 < res.size() && res[i] == res[i + 1]) {
                    ++cnt;
                    ++i;
                }
                cur += to_string(cnt) + res[i];
            }
            res = cur;
        }
        return res;
    }
};

  

  

  

原文地址:https://www.cnblogs.com/lightwindy/p/9625347.html

时间: 2024-10-07 15:57:37

[LeetCode] 38. Count and Say 计数和读法的相关文章

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say 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" o

Count and Say 计数和读法

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

leetCode 38. Count and Say 字符串

38. Count and Say 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" 

LeetCode 38 Count and Say(字符串规律输出)

题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111221—>312211—>…. 按照上面的规律进行求解出第n个字符串是什么. 规律:相连的数字有多少个然后添加上这个数字 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 按照规律进行求解字符串 */ public class So

5.Leetcode 38:Count and Say 笔记

1:题目描述 Given an integer n, generate the nth term of the count-and-say sequence. 找规律,给定一个数n后,写出第n行的字符串 2:题目分析 突然发现自己的Say实在不发达,看了半个多小时才知道咋回事.n就是再说n-1行的内容:"有'x1'个'num1'和'x2'个'num2'--",分析完毕,开始写代码 3:解题思路 1 class Solution(object): 2 def countAndSay(se

Java [leetcode 38]Count and Say

题目描述: 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.

[LeetCode 38] Count and Say

题目链接:count-and-say /** * 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 on

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: 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 121

19.2.3 [LeetCode 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