leetcode — count-and-say
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的更多相关文章
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [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 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- LeetCode——Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
随机推荐
- idea取消vim模式
在安装idea时选择了vim编辑模式,但是用习惯了eclipse,总是要拷贝粘贴,在idea中一直按ctrl+c和ctrl+v不起总用.于是想把vim模式关闭掉.方法:菜单栏:tools->vi ...
- PHP中逻辑运算符的高效用法---&&和||
偶尔遇到这个,查了一下,所以就摘录了. 逻辑运算符无非是将值进行逻辑运算.还有其它用法吗?首先让我们先看一下下面的代码,然后我们再一起展开话题.提前给出结论就是(&&)or(||)”这 ...
- python3中报错:TypeError: 'range' object doesn't support item deletion
1.源代码 以下代码执行时会报 range' object does not support item assignment 的错误,问题出现在第17行的runge(10): import unit ...
- LOJ-10092(最大半连通子图)
题目连通:传送门 思路: 题目定义很清晰,然后就不会了QAQ…… 后来看了书,先缩点,然后再用拓扑排序找到最长的链子的节点数(因为缩点后所有点都是一个强连通分量,所以找最长的链子就是最大限度包含 点的 ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- MapGIS数据中心设计器 帮助文档
我以为是数据设计呢..数据设计按钮在哪里??? 数据库设计和管理(没mapgis k9那么好找)(可以编辑sql数据,可以开发吗?)文件中肯定是不行的,要网络发布,肯定是要导入sql数据库中的(或者是 ...
- [纪录片] 鸟瞰中国 ——China from Above
上周末我看了一部纪录片<鸟瞰中国>,觉得特别有意思,想要分享给你. 制作背景 <鸟瞰中国>是由中国五洲传播中心与美国国家地理频道联合拍摄的纪录片,由KNNY PNG.KLAUS ...
- day_11函数的形参与实参
昨天讲了函数的定义和简单分类 1:什么是函数:具体特定功能的代码快 --特定功能代码作为一个整体,并给该整体命名,就是函数. 函数的优点: 1:减少代码的冗余 2:结构清晰,可读性强 3:具有复用性, ...
- 音视频编解码——LAME
一.LAME简介 LAME是目前非常优秀的一种MP3编码引擎,在业界,转码成Mp3格式的音频文件时,最常用的就是LAME库.当达到320Kbit/s时,LAME编码出来的音频质量几乎可以和CD的音质相 ...
- 关键字提取算法TF-IDF
在文本分类的学习过程中,在“如何衡量一个关键字在文章中的重要性”的问题上,遇到了困难.在网上找了很多资料,大多数都提到了这个算法,就是今天要讲的TF-IDF. 总起 TF-IDF,理解起来相当简单,他 ...