[LeetCode]Count and Say
来源:互联网 发布:算法mobi 编辑:程序博客网 时间:2024/06/06 16:55
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) { if(n==1) return "1"; String s = countAndSay(n-1); StringBuilder sb = new StringBuilder(); for(int i=0;i<s.length();i++){ char c = s.charAt(i); int k = 0; for(int j=i+1;j<s.length();j++){ if(c==s.charAt(j)){ k++; }else{ break; } } i=i+k; if(k==0){ sb.append("1"+Character.toString(c)); } else{ sb.append(Integer.toString(k+1)+Character.toString(c)); } } return sb.toString(); }}
0 0
- 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
- LeetCode | Count and Say
- 经典游戏算法之骑士走棋盘
- 杭电acm2013蟠桃记
- 哈希表工作原理
- mysql增删改
- Eclipse与Android源码编译过程(针对如何将第三方so文件打包进apk中)
- [LeetCode]Count and Say
- 笔试分类总结
- fopen 参数详解
- hdu5009 Paint Pearls DP
- 东软学习,strut2初级学习马士兵的部分截图和笔记(一)
- AOP技术应用和研究--OOP
- RQNOJ:PID5 能量项链
- poj 1129 Channel Allocation(图着色,DFS)
- 教你透彻了解红黑树