import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/count-and-say/
*
* Created by lverpeng on 2017/7/14.
*
* 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.
*
*/
public class CountAndSay { /**
* 循环n次,根据上一个字符串找到下一个
*
* @param n
* @return
*/
public String countAndSay (int n) {
if (n == 0) {
return "1";
}
if (n == 1) {
return "11";
}
List<String> list = new ArrayList<String>();
list.add("1");
list.add("11");
for (int i = 2; i <= n; i++) {
getNext(list);
} System.out.println(Arrays.toString(list.toArray()));
return list.get(list.size() - 1);
} private void getNext (List<String> list) {
String last = list.get(list.size() - 1);
String next = "";
int count = 1;
for (int i = 1; i < last.length(); i++) {
if (last.charAt(i) == last.charAt(i - 1)) {
count ++;
if (i == last.length() - 1) {
next += count + "" + last.charAt(i - 1);
}
} else {
next += count + "" + last.charAt(i - 1);
count = 1;
if (i == last.length() - 1) {
next += count + "" + last.charAt(i);
}
}
}
list.add(next);
} public static void main(String[] args) {
CountAndSay countAndSay = new CountAndSay();
System.out.println(countAndSay.countAndSay(2));
System.out.println(countAndSay.countAndSay(3));
System.out.println(countAndSay.countAndSay(4));
System.out.println(countAndSay.countAndSay(5));
System.out.println(countAndSay.countAndSay(6));
}
}

leetcode — count-and-say的更多相关文章

  1. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  2. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  3. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  4. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  5. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  6. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  8. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  9. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  10. LeetCode——Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

随机推荐

  1. SAS 函数

    SAS 函数 SAS函数是编程语言的一个组件,可接受参数.执行计算或进行其他操作并返回值.返回值是字符型或数值型的结果,可用于赋值语句或 表达式中.SAS包含很多函数,也可以自定义函数.在BASE S ...

  2. JDK工具 javap

    javap -c [ClassName]  编译为汇编语言

  3. linux从0开始----01

    1.VMware 虚拟机安装与卸载 推荐安装较高版本,11.x以后的.本课程安装12.x版本,需要序列号. 2.在vmware中安装centos客户机.初学者选择典型安装也可以. 1.vware文件菜 ...

  4. 高级查询query

    详细看 https://www.kancloud.cn/ldkt/tp5_db/229042

  5. UE4行为树

    这是 UE4中行为树编辑器 中可用的默认节点.取决于开发项目的不同(如射击游戏),可能会有更多节点.这里介绍五种行为树节点类型:   节点类型 描述 Composite(流程控制节点) 这种节点定义一 ...

  6. php项目中使用element.ui和vue

    1.plugins中添加axios,element-ui 2.全局文件下引入 <script src="/static/plugins/vue@2.5.13/vue.js"& ...

  7. JVM中的堆和栈

    基本概念: 基本数据类型:byte   short   int   long  char  float  double  boolean 引用数据类型:类类型.接口类型和数组 栈内存: 程序在栈内存中 ...

  8. HTTP二、HTTP请求处理过程的七个步骤

      HTTP02 HTTP请求处理过程的七个步骤     1.web服务处理步骤 web服务的处理过程可总结为七个步骤:   1)发起请求:客户端向服务器端发起连接请求,建立”三次握手“: 2)接收请 ...

  9. Exp3 免杀原理与实践_05齐帅

    Exp3 免杀原理与实践 20154305_齐帅 想要弄懂免杀,一定得先把基础问题弄明白啊~~ 一.基础问题回答 (1)杀软是如何检测出恶意代码的? - -检测特征码: 依靠分析总结出计算机病毒中常出 ...

  10. PHP单一文件入口框架简析

    <?php /** * PHP单一文件框架设计简析 * 1.MVC架构实现 * 2.URL路由原理 */ //URL路由原理 /** * 路由作用 * 获取url中的c和a变量,执行c类对应的方 ...