leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是
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的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [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 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- 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 ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
随机推荐
- Spark运行环境的安装
scala-2.9.3:一种编程语言,下载地址:http://www.scala-lang.org/download/ spark-1.4.0:必须是编译好的Spark,如果下载的是Source ...
- 黑马程序员——C语言基础 scanf函数 基本运算 三目运算符
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)scanf函数 1> 简单介绍一下scanf函数 这是在 ...
- 【转发】du命令 实现Linux 某个文件夹下的文件按大小排序
1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...
- 无刷新 checkbox列表的删除
前台 JS : function ModelDelete() { var checkvalues = null; var checValue = $("#dom1").find(& ...
- oracle中的cluster表
大家对通常oracle中的cluster的理解是不准确的,经常和sql server中的cluster index混淆.Cluster是存储一组table的一种方法,这些table共享同一数据块中的某 ...
- hdu 2029
PS: 逻辑问题... 代码: #include "stdio.h"#include "string.h"int main(){ char a[110]; i ...
- JS监听关闭浏览器事件
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
- python简介-copy
首先python的老家https://www.python.org/ 原文http://www.runoob.com/python/python-intro.html Python 简介 Python ...
- Android布局居中的几种做法
Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法: android:layout_gravity android:gravity and ...
- 如何用JS获取ASP.net中的textbox的值 js获不到text值
<tr> <td class="table_body" style="width: 10%" a ...