38. Count and Say

这个题我有印象,而且印象特别深。

印象就是这个题是个傻逼题,绝对的傻逼题,尤其是描述,极度含糊不清。

第一行是1,一定的

从第二行开始,每一行是数上一行的数。

比如弟N行是22344
第N+1行就是二个2,一个3,二个4 = 221324...

然后每一行根据上一行来构建,他又没说从1开始还是从0开始,反正是个傻逼题。

真心的,我知道我说了好多次傻逼了,但是这个题真的是傻逼题,比轻信EX被绿的我还傻逼。

public class Solution {
    public String countAndSay(int n) {
        if (n == 1) return "1";
        String res = "1";
        
        for (int i = 1; i < n; i++) {
            int same = res.charAt(0) - '0';
            int num = 1;
            String temp = "";
            for (int j = 1; j < res.length(); j++) {
                if (res.charAt(j) - '0' == same) {
                    num++;
                } else {
                    temp += "" + num + same;
                    num = 1;
                    same = res.charAt(j) - '0';
                }
            }
            temp += "" + num + same;
            res = temp;
        }
        
        return res;
    }
}
posted @ 2016-11-05 12:59  哇呀呀..生气啦~  阅读(107)  评论(0)    收藏  举报