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. FCT需求分析

    1. 系统组成 系统从硬件角度看是由芯片.电源,时钟,总线组成, 当中总线分为控制总线和数据总线. 芯片是单个的硬件单元,可实现多种功能.有些功能有性能需求,在计算机系统中大部分功能都须要软件配合. ...

  2. 中国城市线划分—Where do you want to develop......

  3. 深入了解PHP闭包的使用以及实现

    一.介绍 匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用 ...

  4. Swing(Java)--维基百科

    Swing是一个为Java设计的GUI工具包.Swing是Java基础类的一部分.Swing包括了图形用户界面(GUI)组件如:文本框,文本域,按钮,分隔窗格和表. Swing提供许多比AWT更好的屏 ...

  5. Python实现天数倒计时计算

    tips:在datetime模块里有一个计算时间差的 timedelta.让两个datetime对象相减就得到timedelta ###--Python实现天数倒计时计算 #tips:在datetim ...

  6. CocoaPods的ruby问题 Error fetching http://ruby.taobao.org/:

    今天安装了一个CocoaPods,在安装淘宝ruby是遇到了问题 bogon:~ zhch$ gem sources -a http://ruby.taobao.org/ Error fetching ...

  7. 马老师 LNMP生产环境Web架构 笔记

    http协议和缓存原理.多路IO模型: MIME机制,Multipurpose Internet Mail Extensions,多用户互联网邮件扩展.MIME使用一个简单的字符串组成,最初是为了标识 ...

  8. Java虚拟机学习 - 内存调优 ( 9 )

    JVM调优主要是针对内存管理方面的调优,包括控制各个代的大小,GC策略.由于GC开始垃圾回收时会挂起应用线程,严重影响了性能,调优的目是为了尽量降低GC所导致的应用线程暂停时间. 减少Full GC次 ...

  9. jQuery瀑布流无限拖三大利器:masonry+imagesloaded+infinitescroll

    瀑布流已经是几乎过时的技术了,不过对于很多想要快速实现它的朋友而言,却绝非易事,因为即使我们已经有很多现成的代码,却发现在自己的开发环境中无法快速得到自己想要的结果.就像我们现在要介绍的三大利器(ma ...

  10. .Net4.0 任务(Task)[转]

    .Net4.0 任务(Task) 任务(Task)是一个管理并行工作单元的轻量级对象.它通过使用CLR的线程池来避免启动专用线程,可以更有效率的利用线程池.System.Threading.Tasks ...