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。

原题链接:https://oj.leetcode.com/problems/count-and-say/

能够看出,后一个数字是前个数字的读法,21读作1个2,先写下12,1个1,再写下11,连起来1211.

	public static String countAndSay(int n) {
if(n <= 0)
return null;
String str = "1";
int count = 1;
for(int i=0;i<n-1;i++){
StringBuilder builder = new StringBuilder();
for(int j=0;j<str.length();j++){
if(j<str.length()-1 && str.charAt(j)==str.charAt(j+1))
count++;
else{
builder.append(count + "" + str.charAt(j));
count = 1;
}
}
str = builder.toString();
}
return str;
}

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 ...

随机推荐

  1. 通过xml生成word文档

    Xml生成word总结 使用xml生成word的基本步骤在<使用xslt转化xml数据形成word文档导出.doc>中说明比较清楚了.但是其中的细节并未说到,因此自己折腾了两天总算成功了. ...

  2. docker学习笔记4:利用docker hub上的mysql镜像创建mysql容器

    docker hub上有官方的mysql镜像,我们可以利用它来创建mysql容器,作为一个服务容器使用. 1.下载mysql镜像 docker pull mysql 2.创建镜像 docker run ...

  3. loadrunner参数化总结

    Select next row:Sequential,Rondom,Unique Update value on:Each iteration,Each occurrence,Once 下面分别对这两 ...

  4. 半透明panel

    用API  SetLayeredWindowAttributes

  5. Java读写Word文件常用技术

      Java操作操作Word文件,最近花了几天时间解决使用Word模板导出数据的问题,收集到一些资料分享下. 常见的技术如下: 1.POI(兼容doc.docx文件) 官方网站:http://poi. ...

  6. 学习笔记之NodeJs基本操作

    nodejs安装见文章:windows下安装node.js及less 运行js文件:node xxx.js 调用http模块,并指定端口为3000,向客户端输出<h1>Node.js< ...

  7. linux命令:Linux命令大全

    Linux命令大全 http://man.linuxde.net/

  8. 重操JS旧业第六弹:基本类型包装

    在前面已经知道js中的类型有boolean,string,number,undefined,function,object,其中boolean,number,string为值类型.所谓的基本类型包装, ...

  9. 技术回归01-Windows内存分配工具

    很久没有写技术方面的东西了,这半年主要是在学习别人的东西,对自己提高比较大,算是一次技术回笼吧,这次学习之旅目的是结束技术方面的专注,开始向应用方面找突破口,也就是完成技术积累或者为技术的积累做坚实的 ...

  10. PHP学习之-1.1 PHP 可以做什么?

    PHP 可以做什么? 为什么要学习PHP,"我可以用javascript来实现程序的编写."但是javascript的能力是有限的,javascript通常运行在浏览器(客户端), ...