Count and Say(tag:String)
来源:互联网 发布:徕卡ts02plus导出数据 编辑:程序博客网 时间:2024/06/16 17:40
Qustion:
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 12. 113. 214. 12115. 111221
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1Output: "1"
Example 2:
Input: 4Output: "1211"
Solution:
public String countAndSay(int n) { StringBuilder str; StringBuilder res = new StringBuilder(""); int p=0; char sp; char sq; if(n==1) return "1"; else { str=new StringBuilder(countAndSay(n-1)); for(int i=0;i<=str.length();i++) { sp=str.charAt(p); sq=i==str.length()?' ':str.charAt(i); if(sp!=sq||i==str.length()) { res.append((i-p)).append(sp); p=i; } } } return res.toString(); }
总结:
字符串总变化的时候StringBulider比String效率高。
执行速度:StringBuilder>StringBuffer。
线程安全:StringBuilder不安全,StringBuffer安全。
使用选择:1.如果要操作少量的数据用String;
2.单线程操作字符串缓冲区下操作大量数据用StringBuilder;
3.多线程操作字符串缓冲区下操作大量数据用StringBuffer。
阅读全文
0 0
- Count and Say(tag:String)
- [leetcode][string] Count and Say
- String:38. Count and Say
- 【leetcode】String——Count and Say(38)
- String——count and say
- LeetCode(38) Count and Say
- leetcode系列(50)Count and Say
- Leetcode:38. Count and Say(JAVA)
- LeetCode 38. Count and Say(点数)
- leetcode_c++:Count and Say(038)
- 38. Count and Say(unsolved)
- Easy 11 Count and Say(38)
- Easy 12 Count and Say(53)
- 38. Count and Say(第十二周)
- LeetCode--Count and Say(Python)
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- 古生僻词语、成语
- bzoj1636: [Usaco2007 Jan]Balanced Lineup
- 省队集训Round3 DAY4
- 分布式文件系统-FastDFS
- Java写入文件
- Count and Say(tag:String)
- 二狗子的C语言学习之路(数组)
- 特征金字塔特征用于目标检测:Feature Pyramid Networks for Object Detection
- Windows20
- 收邮件NO Select Unsafe Login. Please contact kefu解决办法
- Windows21
- python PIL保存图片出错的替方法
- 天生丽质小仙女 jdbc servlet基础用法下
- bzoj2717: [Violet 4]迷路的兔子