Leetcode-Count and Say

LiBlog發表於2014-11-29

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.

Analysis:

The rule is:

1 -> 11

2 -> 12

3 -> 13

11 -> 21

22 -> 22

33 -> 23

111 -> 31

222 -> 32

333 -> 33

Solution:

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         String curStr = "";
 4         if (n==0) return curStr;
 5         curStr="1";
 6         if (n==1) return curStr;
 7 
 8         for (int i=2;i<=n;i++){
 9             String newStr = "";
10             int len = curStr.length();
11             int index = 0;
12             while (index<len){
13                 char cur = curStr.charAt(index);
14                 index++;
15                 if (cur=='1') {
16                     if (index+1<len && curStr.charAt(index)=='1' && curStr.charAt(index+1)=='1'){
17                         newStr += "31";
18                         index += 2;
19                     } else if (index<len && curStr.charAt(index)=='1'){
20                         newStr += "21";
21                         index++;
22                     } else newStr += "11";
23                 } else if (cur=='2'){ 
24                     if (index+1<len && curStr.charAt(index)=='2' && curStr.charAt(index+1)=='2'){
25                         newStr += "32";
26                         index += 2;
27                     } else if (index<len && curStr.charAt(index)=='2'){
28                         newStr += "22";
29                         index++;
30                     } else newStr += "12";
31                 } else {
32                     if (index+1<len && curStr.charAt(index)=='3' && curStr.charAt(index+1)=='3'){
33                         newStr += "33";
34                         index += 2;
35                     } else if (index<len && curStr.charAt(index)=='3'){
36                         newStr += "23";
37                         index++;
38                     } else newStr += "13";
39                 }
40             }
41             curStr = newStr;
42         }
43 
44         return curStr;       
45     }
46 }

 Solution 2:

Solution 1 is a naive solution. Even though the number in the string cannot exceed 4, we should figure out a general solution.

 1 public class Solution {
 2     public String countAndSay(int n) {
 3         String res = "1";
 4         for (int i=2;i<=n;i++){            
 5             int index = 0;
 6             StringBuilder buf = new StringBuilder();
 7             while (index<res.length()){
 8                 int count = 1;
 9                 int index2 = index+1;
10                 while (index2<res.length() && res.charAt(index2)==res.charAt(index)){
11                     index2++;
12                     count++;
13                 }
14 
15                 buf.append((char)(count+'0'));
16                 buf.append(res.charAt(index));
17                 index = index2;
18             }
19             res = buf.toString();
20         } 
21 
22         return res;
23     }   
24 }

 

相關文章