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 ...
随机推荐
- Cannot load supported formats: Cannot run program "svn"
Cannot load supported formats: Cannot run program "svn" CreateTime--2018年4月26日11:32:37 A ...
- m2a-vm超频的方法
m2a-vm超频的方法 此帖对"华硕超频俱乐部"的评论 要超频的话,bios要进行相应的设置, 1.jumperfree选项下 cpu multiplier 改cpu的倍频 ...
- openfire + spark 展示组织机构(客户端)
在spark 加一个插件用于展示组织机构, 参考了好多人的代码 插件主类增加一个 TAB用于展示机构树 package com.salesoa.orgtree; import java.net.URL ...
- 捕获Chrome浏览器全屏退出事件
参考地址 document.addEventListener("fullscreenchange", function(e) { console.log("fullscr ...
- nginx中的502错误
遇到这种情况,首先看一下慢日志 [17-Aug-2015 13:13:43] WARNING: [pool www] child 27780, script '/data/s.com/index.ph ...
- HDUOJ ----1709
The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HttpClient中的Timout
connection timeout和SoTimeout A connection timeout occurs only upon starting the TCP connection. This ...
- 何时使用copy,strong,weak,assign关键字 定义属性
现在我们看看iOS5中新的关键字strong, weak, unsafe_unretained. 可以与以前的关键字对应学习strong与retain类似,weak与unsafe_unretained ...
- linux 浏览查看文件more,less,head,tail,cat,tac,od,nl命令使用简介
参考:linux 基本命令详解 cat,tac,nl,more,less,head,tail,od 命令more,less,head,tail,cat,tac,od,nl等是是使用Linux系统常用的 ...
- android检测网络连接状态示例讲解
网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置 Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(Andro ...