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 ...
随机推荐
- android button text属性中英文大小写问题
Android版本升级的原因,需要手动添加属性android:textAllCaps="false"
- 基于K2 BPM的大型连锁企业开关店选址管理解决方案
业内有句名言:“门店最重要的是什么?第一是选址,第二是选址,第三还是选址” 选址是一个很复杂的综合性商业决策过程,需要定性考虑和定向分析.K2开关店&选址管理方案重点关注:如何开出更好的店?在 ...
- CSS引入外部字体
@font-face { font-family: '综艺体'; font-style: normal; font-weight: normal; src: url(../cs ...
- virtualbox安装提示出现严重错误解决办法
解决办法: 在服务里面启动1. Device Install Service2. Device Setup Manager 这两个服务就好了.也有可能只需要启动第一个.
- GPT分区基础知识及如何在GPT分区上安装WIN7
大硬盘和WIN8系统,让我们从传统的BIOS+MBR模式升级到UEFI+GPT模式,现在购买的主流电脑,都是预装WIN8系统,为了更好的支持2TB硬盘,更快速的启动win8,预装系统都采取了GPT分区 ...
- web安全测试-AppScan使用分享
这里主要分享如何使用AppScan对一大项目的部分功能进行安全扫描. ----------------------------------------------------------------- ...
- String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...
- What is the difference Apache (Http Server) and Tomcat (Servlet Container)
The Apache Project The Apache Project is a collaborative software development effort. Its goal is to ...
- 使用RBL拦截垃圾邮件
1. sbl-xbl.spamhaus.org 2 bl.spamcop.net 3 zen.spamhaus.org
- IO复用(Reactor模式和Preactor模式)——用epoll来提高服务器并发能力
上篇线程/进程并发服务器中提到,提高服务器性能在IO层需要关注两个地方,一个是文件描述符处理,一个是线程调度. IO复用是什么?IO即Input/Output,在网络编程中,文件描述符就是一种IO操作 ...