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. mysql 统计某个月每天的数据

    select SUM(order_money) as money,substr(t.pay_time,1,10) as time  from pay_log t where  t.pay_time l ...

  2. 十分钟带你读懂《增长黑客》zz

    背景 “If you are not growing, then you are dying. ”(如果企业不在增长,那么就是在衰亡!) 这句话适用于企业,也适用于个人.人生毕竟不像企业,是非成败,似 ...

  3. LwIP-网络接口管理

    netif      各种类型网络接口的抽象------------------netif.c  netif.h ————netif.h————————————————//网络接口最大物理地址长度,这 ...

  4. Cbv源码简单分析图

    重点:cbv源码的简单分析(有后续)

  5. 无知小子踏入python web大门

    学习python flask的第一天,费劲我小白的脑子,总算完成了环境配置 详情如下,其他小白误走弯路,希望和我一样爱好python的人坚信:python或许是最好的语言! 那么,开始喽,有不对的地方 ...

  6. 继承Thread类和实现Runnable接口

    一.采用继承Thread类方法的特点: 优势:编写简单,如果需要访问当前的线程,只需要使用this,并可以在run()方法中调用其他线程的方法: 劣势:线程已经继承了Thread类,不能继承其他的父类 ...

  7. 安卓端 - H5页面在微信分享、收藏、保存图片不成功

    经过代码实践: 原因是微信在分享.收藏和保存时会获取到图片信息,当图片过大时,造成失败

  8. Express实例代码分析1——简单的用户验证登录文件

    /** * Module dependencies. */ var express = require('../..');// ../..是上级目录的上级目录 var hash = require(' ...

  9. JSTL配置文件下载地址

    Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载地址:http:/ ...

  10. C#.NET开源项目、机器学习、Power BI

    [总目录]本博客博文总目录-实时更新   阅读目录 1.开源Math.NET基础数学类库使用系列 2.C#操作Excel组件Spire.XLS文章目录 3.彩票数据资料库文章 4.数据挖掘与机器学习相 ...