这道题主要就是求一个序列,题目得意思就是

1 --> 11 --> 21 --> 1211 -->     111221 -->       312211 --> .....

  1个1     2个1     1个2,1个1   1个1,1个2,2个1    3个1,2个2,1个1  依次类推

题目很简单,但是为了得到较好的结果,还是纠结了一段时间

public class countAndSay {
public String countAndSay(int n) {
String num = "1";
for(int m = 1; m<n ; m++ ){
num = getString(num);
}
return num;
}
public String getString(String num){
StringBuffer result = new StringBuffer();
int j = 0 , i = 0 , n = 0;
while( i< num.length()){
char ch = num.charAt(i);
while( j < num.length() && ch == num.charAt(j) )
j++;
n = j - i;
result.append(n).append(ch-'0');
i = j;
}
return result.toString();
}
}

所以得出的结论就是    1:StringBuffer 在有些情况下优于 String  主要就是在对一个字符串进行多次操作的时候应该用StringBuffer,速度较快

                2:就是ch == num.charAt(j) 与 num.charAt(j) == ch 并不一样,前者要优于后者。

leetcode 38 Count and Say ---java的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. Java [leetcode 38]Count and Say

    题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...

  3. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  4. [LeetCode] 38. Count and Say 计数和读法

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

  5. [leetcode]38. Count and Say数数

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

  6. [LeetCode] 38. Count and Say_Easy

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

  7. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  8. LeetCode - 38. Count and Say(36ms)

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

  9. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

随机推荐

  1. Hibernate中的一对一映射

    1.需求 用户和身份证是一一对应的关系. 有两种对应方式: 用户id作为身份证表的外键,身份证号作为主键: 用户id作为身份证表的主键: 2.实体Bean设计 User: public class U ...

  2. Web体系=资源+URI+表示

    概述 Web有三个核心概念:资源(Resource).URI(UniformResource Identifer,统一资源标识符).表示(Representation).一个资源由一个URI进行标识. ...

  3. 去除hadoop启动时的警告

    hadoop启动的时候,会出现以下警告提示: 执行more start-all.sh查看该文件 但/libexec下不存在hadoop-config.sh文件,所以会执行bin/hadoop-conf ...

  4. STM32之延时秒,毫秒,微秒

    #include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...

  5. JAVA SERVLET专题(上)

    SERVLET简介 ·Java Servlet 是和平台无关的服务器端组件,它运行在Servlet容器中.Servlet容器负责Servlet和客户的通信以及调用Servlet的方法,Servlet和 ...

  6. Java中的blank final

    Java allows the creation of blank finals, which are fields that are declared as final but are not gi ...

  7. 一、XML语法

    xml声明xml指令:<? ?>xml编码与乱码xml元素(标签)CDATA区空格与换行会被认为是标签的内容xml-stylesheet指令解析xml内容 <?xml version ...

  8. struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

    我的前台页是这样的: <body>      <form action="test.action" method="post">     ...

  9. Android中Preference的使用以及监听事件分析

    在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...

  10. HDOJ-三部曲-多重背包-1014-Cash Machine

    通过这道题我基本了解了利用二进制对多重背包问题进行优化的思想. Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...