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. 在datasnap 中使用unidac 访问数据(客户端)

    前面我们讲了如何使用unidac 在datasnap 的服务端访问数据库,今天大概讲一下客户端如何访问 前面做的服务器?其实这个客户端适合任何datasnap 服务端. 首先我们建一个应用,并加入一个 ...

  2. 移动端各种滚动场景需求的插件better-scroll

    移动端各种滚动场景需求的插件: 文档地址: better-scroll:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better- ...

  3. Oracle 12c 安装问题及解决方案

    1. 介绍 今天在我的开发电脑上安装Oracle12c,电脑环境是windows10家庭中文版,安装的Oracle数据库版本Oracle(12.1.0.2.0) - Standard Edition ...

  4. 2019.03.25 bzoj2329: [HNOI2011]括号修复(fhq_treap)

    传送门 题意简述: 给一个括号序列,要求支持: 区间覆盖 区间取负 区间翻转 查询把一个区间改成合法括号序列最少改几位 思路: 先考虑静态的时候如何维护答案. 显然把所有合法的都删掉之后序列长这样: ...

  5. Python_day8

    多态 class Animal(object): def run(self): print('animal is running') class Dog(Animal): def run(self): ...

  6. 6 week work 3

    sticky vs fixed sticky:表示粘贴到某个位置.当组件设置了该属性值后,当页面滑动时,组件会跟着页面移动,当组件触及到窗体后,页面若继续滑动,组件则处在与窗体接触的位置不动.元素的定 ...

  7. kSum问题总结

    1.2Sum 题目: 方法一:两次迭代 public class TwoSum { public static int[] twoSum(int[] nums, int target) { int[] ...

  8. linux系统下安装redis以及java调用redis

    关系型数据库:MySQL  Oracle 非关系型数据库:Redis 去掉主外键等关系数据库的关系性特性 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2. ...

  9. day_7数据类型的相互转换,与字符编码

    首先复一下昨天的内容 1:深浅拷贝 1:值拷贝 直接赋值 列表1=列表2       列表1中的任何值发生改变,列表2中的值都会随之改变 2:浅拷贝,列表2=列表1  列表1中存放的值的地址没有改变, ...

  10. 卷积神经网络中的channel 和filter

    在深度学习的算法学习中,都会提到 channels 这个概念.在一般的深度学习框架的 conv2d 中,如 tensorflow .mxnet,channels 都是必填的一个参数. channels ...