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. 【svn】Centos搭建svn服务器环境

    1.需求描述 在Centos系统中搭建svn服务器环境 2.搭建过程 2.1 yum安装svn [root@localhost /]# yum install svn  2.2 新建目录存储svn目录 ...

  2. SpringMVC 类内部的RequestMapping注解能否被继承?

    首先注意标题,说的是类内部的注解 结论是: 不能,但是子类却可以享有父类中该注解带来的效果. 看了一下这个:http://elf8848.iteye.com/blog/1621392 自己也试了一下, ...

  3. Windows客户端的JProfiler远程监控Linux上的Tomcat

    1.测试环境 服务器:RedHat Linux 3.4.3-9.EL4(内核版本 2.6.9-5.EL),Tomcat5.5.20,Sun JDK 1.5.0_09,JProfiler 4.3.2 f ...

  4. git编译

    Git 是一个自由.开源.高效的分布式版本控制系统(VCS),它是基于速度.高性能以及数据一致性而设计的,以支持从小规模到大体量的软件开发项目.Git 是一个可以让你追踪软件改动.版本回滚以及创建另外 ...

  5. Java I/O 应用程序设计

    课程  Java面向对象程序设计 一.实验目的 掌握数据流的使用 二.实验环境 1.微型计算机一台 2.WINDOWS操作系统,Java SDK,Eclipse开发环境 三.实验内容 1.使用Scan ...

  6. (转)Stack Overflow 2016最新架构探秘

    这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 20 ...

  7. A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误

    感觉很奇怪,查看了一下磁盘,发现workspace所在磁盘已经满了,删除一些文件之后,选择项目->Project->Clean...->选择Clean all projects-&g ...

  8. 转 Python标准库01 正则表达式 (re包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我将从正则表达式开始讲Python的标准库.正则表达式是文字处理中常用的工具,而且 ...

  9. php log类

    个人抽离出来一个超级简易但是实用的代码段 public static function WriteLog($msg,$module = null,$logLevel = "DEBUG&quo ...

  10. 中国计算机学会CCF推荐国际学术期刊会议(最新版)

    中国计算机学会推荐国际学术期刊会议 2014年12月,中国计算机学会(CCF)启动新一轮<)计算机体系结构/高性能计算/存储系统: )计算机网络:)网络与信息安全:)软件工程/系统软件/程序设计 ...