【Leetcode】38. Count and Say

Question:

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, 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"

Leedcode提交代码:Runtime: 19 ms

StringBuilder curr=new StringBuilder("1");
        StringBuilder ans;
        //if(n==1) return "1";
        int temp;
        char a;
        for(int i=1;i<n;i++){//循环n
            ans=curr;
            curr=new StringBuilder();
            temp=1;
            int length=ans.length();
            a=ans.charAt(0);
            for(int j=1;j<length;j++){//循环上一个ans
                
                if(ans.charAt(j)!=a){
                    curr.append(temp).append(a).toString();
                    temp=1;
                    a=ans.charAt(j);
                    //System.out.println(ans);
                }else{
                    temp++;
                }
                
            }
            curr.append(temp).append(a);
        }
        System.out.println(curr);
        return curr.toString();

完整可运行代码:

package easy;

public class L38 {
    public String countAndSay(int n) {
        StringBuilder curr=new StringBuilder("1");
        StringBuilder ans;
        //if(n==1) return "1";
        int temp;
        char a;
        for(int i=1;i<n;i++){//循环n
            ans=curr;
            curr=new StringBuilder();
            temp=1;
            int length=ans.length();
            a=ans.charAt(0);
            for(int j=1;j<length;j++){//循环上一个ans
                
                if(ans.charAt(j)!=a){
                    curr.append(temp).append(a).toString();
                    temp=1;
                    a=ans.charAt(j);
                    //System.out.println(ans);
                }else{
                    temp++;
                }
                
            }
            curr.append(temp).append(a);
        }
        System.out.println(curr);
        return curr.toString();
    }

    public static void main(String[] args) {
        L38 l38 = new L38();
        l38.countAndSay(9);
    }
}

注:

①最开始使用一个String变量ans进行循环,导致每轮次ans之间混乱,加入curr变量,解决这个问题。

② String与StringBuilder之间的区别:

    String:字符串常量

    StringBuffer:字符串变量

String s = "abcd";//创建名为s的String类型变量 abcd
s = s+1;//创建新的对象s 赋值为abcd1
System.out.print(s);// result : abcd1

    StringBuilder:线程非安全的

    StringBuffer:线程安全的

              1.如果要操作少量的数据用 = String

                        2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder

                        3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer

当stringBuilder全部变成String可运行代码:Runtime: 63 ms

String curr=new String("1");
        String ans;
        //if(n==1) return "1";
        int temp;
        char a;
        for(int i=1;i<n;i++){//循环n
            ans=curr;
            curr=new String();
            temp=1;
            int length=ans.length();
            a=ans.charAt(0);
            for(int j=1;j<length;j++){//循环上一个ans
                
                if(ans.charAt(j)!=a){
                    curr+=temp;
                    curr+=a;
                    temp=1;
                    a=ans.charAt(j);
                    //System.out.println(ans);
                }else{
                    temp++;
                }
                
            }
            curr+=temp;
            curr+=a;
        }
        System.out.println(curr);
        return curr;

 

posted @ 2017-06-27 22:08  于淼  阅读(153)  评论(0编辑  收藏  举报