LeetCode-38. Count and Say

来源:互联网 发布:医技软件 编辑:程序博客网 时间:2024/06/06 03:09

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     12.     113.     214.     12115.     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: 1Output: "1"

Example 2:

Input: 4Output: "1211"

题意:
n=1时输出字符串1;
n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;
n=3时,由于上次字符是11,有2个1,所以输出21;
n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。
依次类推,写个countAndSay(n)函数返回字符串。

package solutions._38;/** * 38. Count and Say */class Solution {    private StringBuilder sb = new StringBuilder();    private String DFS(int i, int n) {        if (n == 1) {            return "1";        }        if (i == n) {            return sb.toString();        }        char[] arr = sb.toString().toCharArray();        sb.delete(0, sb.length());        int j = 0;        int count = 1;        while (j < arr.length - 1) {            if (arr[j] == arr[j + 1]) {                count++;            } else {                sb.append(count);                sb.append(arr[j]);                count = 1;            }            j++;        }        if (arr.length == 1) {            sb.append(1);            sb.append(arr[0]);        } else if (arr.length >= 2) {            if (arr[arr.length - 1] != arr[arr.length - 2]) {                sb.append(1);                sb.append(arr[arr.length - 1]);            } else {                sb.append(count);                sb.append(arr[arr.length - 1]);            }        }        return DFS(i + 1, n);    }    public String countAndSay(int n) {        sb.delete(0, sb.length());        sb.append(1);        return DFS(1, n);    }    public String countAndSay2(int n) {        if (n == 1) {            return "1";        }        char[] str = (countAndSay2(n - 1) + "*").toCharArray();        int count = 1;        StringBuilder sb = new StringBuilder();        for (int i = 0; i < str.length - 1; i++) {            if (str[i] == str[i + 1]) {                count++;            } else {                sb.append(count);                sb.append(str[i]);                count = 1;            }        }        return sb.toString();    }    public String countAndSay3(int n) {        if (n == 1) {            return "1";        }        StringBuilder sb = new StringBuilder();        sb.append("1");        for (int i = 2; i <= n; i++) {            int count = 1;            char[] arr = (sb.toString() + "*").toCharArray();            sb.delete(0, sb.length());            for (int j = 0; j < arr.length - 1; j++) {                if (arr[j] == arr[j + 1]) {                    count++;                } else {                    sb.append(count);                    sb.append(arr[j]);                    count = 1;                }            }        }        return sb.toString();    }    public static void main(String[] args) {        Solution solution = new Solution();//        System.out.println(solution.countAndSay(5));        //System.out.println(solution.countAndSay2(5));        System.out.println(solution.countAndSay3(5));    }}
原创粉丝点击