zoukankan      html  css  js  c++  java
  • 【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;
  • 相关阅读:
    luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
    codeforces 600E . Lomsat gelral (线段树合并)
    bzoj 1483: [HNOI2009]梦幻布丁 (链表启发式合并)
    bzoj 1257: [CQOI2007]余数之和 (数学+分块)
    codevs 2606 约数和问题 (数学+分块)
    bzoj 2038: [2009国家集训队]小Z的袜子(hose) (莫队)
    bzoj 1086: [SCOI2005]王室联邦 (分块+dfs)
    bzoj 4542: [Hnoi2016]大数 (莫队)
    【NOIp模拟赛】Tourist Attractions
    【NOIp模拟赛】String Master
  • 原文地址:https://www.cnblogs.com/yumiaomiao/p/7087375.html
Copyright © 2011-2022 走看看