38. Count and Say
来源:互联网 发布:淘宝上那个吉他店最好 编辑:程序博客网 时间:2024/06/06 10:08
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 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 sequence.
Note: The sequence of integers will be represented as a string.
public class Solution { public String countAndSay(int n) { String[] strs = new String[n];strs[0] = "1";StringBuilder sb = new StringBuilder();int sum = 0;char ch;for (int i = 1; i < n; i++) {ch = strs[i - 1].charAt(0);for (int j = 0; j < strs[i - 1].length(); j++) {if (strs[i - 1].charAt(j) != ch) {sb.append("" + sum + ch);ch = strs[i - 1].charAt(j);sum = 1;} else {sum++;}if(j==strs[i - 1].length()-1){sb.append("" + sum + ch);}}strs[i] = sb.toString();sb.delete(0, sb.length());sum=0;}return strs[n - 1]; }}
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- UVA12166 修改天平
- 编译原理中Follow集的求法
- HDU4973 【几何。】
- leetcode--unique_paths
- printf和scanf函数
- 38. Count and Say
- caffe中lmdb和hdf5读取时的总结
- ubuntu系统根目录下各个目录用途说明
- HDU 1524 A Chess Game [SG函数]【博弈】
- CPU多线程处理,一种隐藏传输时间方法
- Canvas 时钟
- hdu 3920 Clear All of Them I (状态压缩DP)
- Solr-----6、SolrJ基本操作
- 58. Length of Last Word (计算最后一个单词的长度)