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;
  • 相关阅读:
    站点防攻击之禁止国外ip
    docker 安装rabbitMQ
    【转】浅析支付系统的整体架构
    【转】MQ详解及四大MQ比较
    SQL server 中的dbo、guest
    网站加载速度的优化(1):优化前端页面
    【转】Apache ab压力测试
    Asp.Net Core 部署发布后EFCore数据库断连和Google浏览器无法存储Cookie
    C# 使用nuget.exe发布类库及更新类库
    .net core 读取Excal文件数据及注意事项
  • 原文地址:https://www.cnblogs.com/yumiaomiao/p/7087375.html
Copyright © 2011-2022 走看看