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 ...
随机推荐
- 使用 C# 开发智能手机软件:推箱子(十二)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第十二篇.在这篇文章中,介绍 Window/AboutDlg.cs 源程序文件. 这个源程序文件包括 AboutDlg 类,该 ...
- exeption ORA-00907: missing right parenthesis
exeption ORA-00907: missing right parenthesis CreationTime--2018年8月16日11点11分 Author:Marydon 1.情景展示 ...
- google kaptcha 验证码组件使用简介
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
- win7硬盘安装方法
Windows 7 完全硬盘安装方法 干干净净的C盘 事先准备:装好系统的电脑一台,win 7安装iso. 注: (1).Windows 7要求安装在NTFS分区,但是其他分区可以是FAT32格式! ...
- 同域名不同端口应用共享sessionid问题解决办法
相同域名共享sessionid 如 www.abc.com www.abc.com/test www.abc.com:8080 www.abc.com:8989 这些地址由于是在相同的域名下,所以共享 ...
- Windwos在cmd如何复制文本
生活的琐事,总是要解决. 01.Win+R打开运行窗口 cmd--回车 02. 勾选快速编辑模式 注意: 快速编辑模式就是可以Ctrl+c(复制).Ctrl+v(粘贴)
- HDUOJ----4006The kth great number(最小堆...)
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- PowerDesigner 的常用方法
http://www.cnblogs.com/studyzy/archive/2008/01/23/1050194.html PowerDesigner 的常用方法 修改外键命名规则 选择Databa ...
- Android WebDriver 浏览器自动测试工具介绍
Selenium WebDriver 是浏览器自动测试工具,提供轻量级和优雅的方式来测试web应用.Selenium WebDriver作为Android SDK extra,支持Android 2. ...
- X-Forwarded-For的一些理解
X-Forwarded-For 是一个 HTTP 扩展头部,主要是为了让 Web 服务器获取访问用户的真实 IP 地址(其实这个真实未必是真实的,后面会说到). 那为什么 Web 服务器只有通过 X- ...