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 where 1 ≤ n ≤ 30, 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”
解法:
核心是遍历前一个字符串,找到重复元素的个数,然后字符串为重复元素的个数和重复元素本身的结合
输入4:
用–n作为循环的条件,因为我们初始字符串为“1”,只需要循环n-1次
第一次循环:str 为“1”,重复元素只有1个,最后返回“11”,即重复数量+本身的值
第二次循环:str 为“11”,重复元素有2个,最后返回“21”,即重复数量+本身的值
第三次循环:str 为“21”,2的重复元素有1个,字符串为“12”,1的重复元素有1个,字符串为“1211”

class Solution {
public:
    string countAndSay(int n) {
        string str = "1";
	    while(--n){
		    string ans = "";
	        int len = str.size();
	        for(int i = 0; i < len; ++i){
                int count = 1;
		        while((i+1) < len && str[i+1] == str[i]) {
                    count++;
                    i++;
                }
	
		        ans += to_string(count) + str[i];
		        
	        }
            str = ans;
	    } 
        return str;
    }
    
    
};

java:

class Solution {
    public String countAndSay(int n) {
        String str = "1";
	    while(--n != 0){
		    String ans = "";
	        int len = str.length();
	        for(int i = 0; i < len; ++i){
                int count = 1;
		        while((i+1) < len && str.charAt(i+1) == str.charAt(i)) {
                    count++;
                    i++;
                }
	
		        ans += (String.valueOf(count)) + str.charAt(i);
		        
	        }
            str = ans;
	    } 
        return str;
        
    }
    
}

python:

  1. Python尽量不要用str,len这些关键词
  2. 与c++ ,java不同,不能简单改成
 for(int i = 0; i < len; ++i){
           int count = 1;
		   while((i+1) < len && str.charAt(i+1) == str.charAt(i)) {
              		count++;
                    i++;
          }
  }
for i in xrange(len):
                
           count = 1
            while (i+1) < len and str[i+1] == str[i]:
             		count+=1
                    i+=1
                
    
           ans += str(count) + str[i]

因为第二个for循环将i的值改了,但for i in xrange(len): 会重新按照0 - len-1的顺序赋值
LeetCode 刷题记录 38. Count and Say_第1张图片
LeetCode 刷题记录 38. Count and Say_第2张图片

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = "1"
        for _ in xrange(n-1):
            
            ans, strlen,i= "", len(res),0
            
            while i < strlen:
                
                count = 1
                while (i+1) < strlen and res[i+1] == res[i]:
                    count+=1
                    i+=1
                
                
                ans += str(count) + res[i]
                i += 1
                
            
            res = ans
        
        return res

你可能感兴趣的:(LeetCode)