【Leetcode Algorithm】Count and Say
来源:互联网 发布:淘宝客服设置分流 编辑:程序博客网 时间:2024/06/18 12:15
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) { //使用StringBuffer来存储第n个序列 StringBuffer sb = new StringBuffer("1"); //从1到n逐次循环,计算第n个序列 for(int i=1;i<n;i++){ int j = 0; //sbTmp用来临时存储第i个序列 StringBuffer sbTmp = new StringBuffer(); while(j<sb.length()){ //用k来记录相同的数字出现的次数 int k = 0; //当出现不同的数时,调出循环 while(++k+j<sb.length()){ if(sb.charAt(j)!=sb.charAt(j+k)){ break; } } //添加次数k sbTmp.append(k); //添加相应的元素 sbTmp.append(sb.charAt(j)); //索引指向下一个元素 j += k; } //更新sb序列 sb = sbTmp; } return sb.toString(); }}
0 0
- 【Leetcode Algorithm】Count and Say
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- Javascript模块化编程(二):AMD规范
- 为抢占印度市场:阿里多线并行祭出“组合拳”
- mysql安装及主从配置
- 深入了解javascript第二篇 this篇
- 安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决
- 【Leetcode Algorithm】Count and Say
- Javascript模块化编程(三):require.js的用法
- Linux的SOCKET编程详解
- iOS RunLoop 初识
- 《C语言及程序设计》程序阅读——条件编译
- drools的JavaDialect异常
- 正则表达式 获取开头结尾的数字
- 在eclipse中android private libraries中关联javadoc
- 使用Loadrunner VuGen编辑器手动编写脚本