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

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. IT公司100题-12-求1+2+…+n

    问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C).   分析: 利用类的静态变量实现: new一含有n ...

  2. winform错误提示 :窗口类名无效(Window class name is not valid)

    winfrom 程序在 xp 操作系统上报错提示 窗口类名无效(Window class name is not valid) 解决方法 注释 Program类 里 这句 Application.En ...

  3. 红帽中出现”This system is not registered with RHN”的解决方案

    原因是你的linux没有在红帽网络上注册,所以无法下载上面的软件包,替代方案可以使用centos. 下面介绍下使用centos 的流程 1.卸载rhel的默认安装的yum包查看yum包rpm -qa| ...

  4. ios开发值json数据文件的存取

    将Json存进本地文件夹   NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  5. 永不消逝的电波(三):低功耗蓝牙(BLE)入门之如何调戏别人的小米手环

    0×00 前言 蓝牙(Bluetooth),一种无线技术标准,用来让固定与移动设备,在短距离间交换数据,以形成个人局域网(PAN).其使用短波特高频(UHF)无线电波,经由2.4至2.485 GHz的 ...

  6. 极客DIY:如何构建一台属于自己的基站

    写在前面(原文作者) 上周我去特拉维夫(Tel Aviv)探望我的朋友结果有了一些收获,一块崭新的BladeRF(x40),即一个支持USB3.0的SDR平台,这就意味着可以同时发送和接收信息了.而H ...

  7. VS2013 JS 跟踪

    VS2013JS  跟踪 1.在页面对应的cs文件中设置断点,F10单步执行. 2.单步执行调试会自然而然执行到JS里面.

  8. Planning for a period of time

    After a period of struggle , i decided to follow the teacher Chen learning . Say true i really disli ...

  9. Helo command rejected: need fully-qualified hostname

    Helo command rejected: need fully-qualified hostname问题 是由于postfix的配置文件(main.cf)有问题.其中有一个smtpd_sasl_l ...

  10. # 20145210 《Java程序设计》第03周学习总结

    教材学习内容总结 第四章 类与对象 在定义类这个小结里,有很多新的术语,书上的比喻很形象,对于理解这部分的内容有很大帮助,现总结如下: •类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象 ...