LeetCode: Count and Say 解题报告
Count and Say
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.

SOLUTION 1:
使用递归来做。这个其实是说要解释几次的问题。
以下这里是解释了3次,你还可以继续用同样的方法继续解释下去,四次五次这样子。
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.
直接看代码吧。也没有什么特别难的地儿。
public class Solution {
public String countAndSay(int n) {
if (n == 0) {
return null;
}
if (n == 1) {
return "1";
}
String s = countAndSay(n - 1);
StringBuilder sb = new StringBuilder();
int len = s.length();
int cnt = 0;
for (int i = 0; i < len; i++) {
cnt++;
if (i == len - 1 || (i < len - 1 && s.charAt(i) != s.charAt(i + 1))) {
sb.append(cnt);
sb.append(s.charAt(i));
cnt = 0;
}
}
return sb.toString();
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/CountAndSay.java
LeetCode: Count and Say 解题报告的更多相关文章
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- 继承Spring AbstractRoutingDataSource实现路由切换
继承Spring AbstractRoutingDataSource实现路由切换 原创 2016年05月11日 16:50:08 标签: mybatis / AbstractRoutingDataS ...
- _x和__all__(有所理解即可)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #_x和__all__(有所理解即可) #_x #不能使用from module import *导入 [注意 ...
- eval、exec、execfile
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://blog.csdn.net/azhao_dn/article/details/6921654 ...
- virtualenv沙箱
准备用Django开发网站,豆瓣阅读中有<Django入门>,里面有人批注说用virtualenv+pip很好. 每次搞个什么都感觉举步维艰,因为之前太懒了,什么都懒得深入看一下.pip一 ...
- Android网络开发之基本介绍
Android平台浏览器采用WebKit引擎,名为ChormeLite,拥有强大扩展特性,每个开发者都可以编写自己的插件. 目前,Android平台有3种网络接口可以使用,分别是:java.net, ...
- http1.1 协议响应方面参数
HTTP1.1 提供了一个必须的Host字段,而且建立好一次连接之后可以重复使用.提高用户的上网体验. 响应信息 HTTP/1.1 200 OK ...
- 我遇到了Hibernate异常
真是郁闷,今天想用Hibernate的实现对数据库的增删查改,但是就是报异常不断啊!呵呵,为什么?就是在主键的问题上,我用主键的生成形式是:Sequence时就报IllegalArgumentExce ...
- HDUOJ-----2571跳舞毯
跳舞毯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Largest Number(leetcode 179)
给定一个int数组(每个数字无前导0),要求用这些数字拼接出一个最大的数字. 解决思路: 对整个数组进行排序,把排序后的结果拼接起来. 那么如何进行排序呢?只需要定义一个比较函数,如果str(x)+s ...
- setContentView(R.layout.activity_main)无法正常引用
今天在写Android代码的过程中,编译器一直报错,错误出在这一行代码: setContentView(R.layout.activity_main) 提示信息是: activity_main can ...