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 解题报告的更多相关文章

  1. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. Unix 网络编程 读书笔记1

    第一章: C/C++语言提供两种不同的编程模式:IPL32和PL64.► IPL32 ● 表示integer/pointer/long三种数据类型是32位(4个字节),在这种模式下,提供32位的地址空 ...

  2. 〖Linux〗VIM youcompleteme 自动补全 #include 文件名称

    1. 拷贝配置文件 cp ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py ~/.vim/.ycm_extra_conf.py 2. 修改配 ...

  3. UI复习练习_优酷布局

    还记得前一周,细致看了一下我自己的代码,特意看了下代码规范,一个好的代码习惯就应该慢慢增加自己寻常练习中. 看看UI吧 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...

  4. Loadrunner脚本编程(2)-VuGen脚本文件的开发过程

    http://www.360doc.com/content/10/0806/13/1698198_44076570.shtml 1.定义测试项目的目标,环境,脚本,测试数据,硬件等.脚本应该符合编码规 ...

  5. PmException--- SQL(统计报表)

    select TAGS,GROUP_CONCAT(TAGS) t from EXCEPTION_RESULT e,PM_TASK t ' and t.OWNER='admin' group by TA ...

  6. 如何显示Firefox插件(Plugins)的完整路径并删除

    在Firefox里面,插件firefox只提供禁用选项,无法直接卸载. 要卸载插件,可以按照下面的步骤: 1.在地址栏内,输入 about:config,回车:在过滤器那一栏输入 plugin.exp ...

  7. python学习笔记011——内置函数__module__、__name__

    1 __module__描述 __module__ : 如果当前模块为顶层模块执行 则打印__main__ 如果当前模块为被调用模块的时候 打印当前模块的名称 2 __module__示例 def f ...

  8. centos7 搭建go环境

    下载go #cd /home #mkdir app #cd app #wget https://storage.googleapis.com/golang/go1.9.linux-amd64.tar. ...

  9. [Android实例] 拖动滑块进行图片拼合验证方式的实现

    该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 拖动滑块进行图片拼合验证方式的实现 现在网站上有各种各样的验证码验证方式,比如计算大小,输入图片内容等,今天在一家网站上看到 ...

  10. PI专利网站

    PI专利网站: 1.http://www.powerint.com/company/patents/trademarks 2.http://patft.uspto.gov/netacgi/nph-Pa ...