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 ...
随机推荐
- 关于linux的添加永久静态路由的static-routes方法
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...
- [C#]记录程序耗时的方法【转发】
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // H ...
- Swift教程_swift常见问题(0005)_完美解决Cannot override 'dealloc'异常
Swift教程_swift常见问题(0001)_CoreData: warning: Unable to load class named 'xxx' for entity 'xxx' Swift教程 ...
- hdoj-1251-统计难题【map】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submiss ...
- EXCEPTION-IBATIS
CreateTime--2016年8月23日08:44:03Author:Marydonibatis的sqlMap的xml文件配置出现的异常信息及解决方案 声明:异常类文章主要是记录了我遇到的异常 ...
- python之模块csv之CSV文件的写入(基本结构)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #CSV文件的写入(基本结构) import csv #csv文件,是一种常用的文本格式,用以存储表格数据,很 ...
- iOS- 快速实现展示布局
概述 比较有规律的页面, 快速实现展示布局, 提高开发效率. 详细 代码下载:http://www.demodashi.com/demo/10713.html 看到这个界面,是不是觉得不像那种比较有规 ...
- 工具-VIM配置
设置缩进的空格数 shiftwidth=4 设置制表符宽度 tabstop=4 高亮显示当前行 cursorline 高亮显示当前列 cursorcolumn
- 【LeetCode】120. Triangle (3 solutions)
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- jQuery+ajax中,让window.open不被拦截(转)
方法1:<input type="button" class="preview" value="预览"/>$('.preview ...