码迷,mamicode.com
首页 > 其他好文 > 详细

Count and Say

时间:2015-03-14 21:32:06      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/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 integer n, generate the nth sequence.

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

解题思路:

这题再次很晦涩,看不懂。看了网友的解释,https://leetcode.com/discuss/7535/examples-of-nth-sequence,第i+1行其实就是将第i行的读法直接写下来。

类似于前面anagram的问题,a个b的结构。于是硬来。

根据i行的结果构建第i+1行。i行的第j+1个char和第j个char相同,计数就++。否则将前面的形式按照“a个b”的格式写入i+1行的结果。

特别要注意遍历到最后一个字符时,无论是否和前面的char相等,都要将结果写入了。

public class Solution {
    public String countAndSay(int n) {
        StringBuffer preLine = new StringBuffer("1");
        
        for(int i = 1; i < n; i++){
            StringBuffer currentLine = new StringBuffer();
            char preChar = preLine.charAt(0);
            int count = 0;
            for(int j = 0; j < preLine.length(); j++){
                if(preLine.charAt(j) == preChar){
                    count++;
                }
                if(preLine.charAt(j) != preChar){
                    currentLine.append(count);
                    currentLine.append(preChar);
                    count = 1;
                    preChar = preLine.charAt(j);
                }
                if(j == preLine.length() - 1){
                    currentLine.append(count);
                    currentLine.append(preChar);
                }
            }
            preLine = currentLine;
        }
        
        return preLine.toString();
    }
}

 或者将最后一个char的处理放在循环外也可以。

public class Solution {
    public String countAndSay(int n) {
        StringBuffer preLine = new StringBuffer("1");
        
        for(int i = 1; i < n; i++){
            StringBuffer currentLine = new StringBuffer();
            char preChar = preLine.charAt(0);
            int count = 0;
            for(int j = 0; j < preLine.length(); j++){
                if(preLine.charAt(j) == preChar){
                    count++;
                }
                if(preLine.charAt(j) != preChar){
                    currentLine.append(count);
                    currentLine.append(preChar);
                    count = 1;
                    preChar = preLine.charAt(j);
                }
            }
            currentLine.append(count);
            currentLine.append(preChar);
            preLine = currentLine;
        }
        
        return preLine.toString();
    }
}

而且,写成下面的形式一定是不对的。

Input: 4
Output: "12"
Expected: "1211"

public class Solution {
    public String countAndSay(int n) {
        StringBuffer preLine = new StringBuffer("1");
        
        for(int i = 1; i < n; i++){
            StringBuffer currentLine = new StringBuffer();
            char preChar = preLine.charAt(0);
            int count = 0;
            for(int j = 0; j < preLine.length(); j++){
                if(preLine.charAt(j) == preChar){
                    count++;
                }
                if(j == preLine.length() - 1 || preLine.charAt(j) != preChar){
                    currentLine.append(count);
                    currentLine.append(preChar);
                    count = 1;
                    preChar = preLine.charAt(j);
                }
            }
            preLine = currentLine;
        }
        
        return preLine.toString();
    }
}

 

Count and Say

标签:

原文地址:http://www.cnblogs.com/NickyYe/p/4338207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!