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

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. python 操作json

    认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...

  2. Struts2 的验证

    概述 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 –基于 XWork Validation Framework 的声明式验证:Struts2 提供了一些基 ...

  3. C++string的操作

    #include <iostream> using namespace std; int main() { //initilization string str("abc.ddd ...

  4. 提升web响应速度的思路

    web响应(主要指加载网页类,不包括大文件下载,看视频)的核心瓶颈在于延迟,不在于带宽. 从感性认知的角度,由于存在tcp的慢启动,所以往往速率还未达到带宽值时,访问就已经结束:另外,没有交互就没有延 ...

  5. C# WinForm程序向datagridview里添加数据

    在C#开发的winform程序中,datagridview是一个经常使用到的控件.它可以以类似excel表格的形式规范的展示或操作数据,我也经常使用这个控件.使用这个控件首先要掌握的就是如何向其中插入 ...

  6. springMVC文件上传(转)

    原文链接: http://www.cnblogs.com/lonecloud/p/5989905.html 在Spring-mvc.xml注入bean 1 <!-- 配置文件上传,如果没有使用文 ...

  7. c规范(1)

    1文件结构 头文件.h 保存文件声明 定义文件.c  程序实现 2版本标示  用注释 (1)版权信息. (2)文件名称,标识符,摘要. (3)当前版本号,作者 修改者,完成日期. (4)版本历史信息. ...

  8. Ogre碰撞检测

    转自:http://blog.csdn.net/weiqubo/article/details/7108363 Ogre采用树桩管理场景中的各种"元素"(摄像机.灯光.物体等),所 ...

  9. (转)JSON数据格式和js操作json总结

    原:http://niutuku.com/tech/javaScript/273643.shtml JSON数据格式和js操作json总结 来源:niutuku.com |         vince ...

  10. 不能使用weak修饰进行声明的类

    These classes include NSTextView, NSFont and NSColorSpace; for the full list, see Transitioning to A ...