The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"

题意:

给定一个数字串,读出来。并把读的方法也表示成一个数字串。如此计算出第N个串。

1)、1

2)、11,表示1)有1个1,组合起来就是11。

3)、21,表示2)有2个1,组合起来就是21。

4)、1211,表示3)有1个2,2个1,组合起来就是1211。

5)、111221,表示4)有1个1,1个2,2个1,组合起来就是111221。

6)、312211,表示5)有3个1,2个2,2个1,组合起来就是312211。

以此类推,求给定n行的输出结果。

思路:

"1  1  1  2  2  1"

j                                                 查看 j 对应值 == j-1 对应值, count更新为2

j                                          查看 j 对应值 == j-1 对应值, count更新为3

j                                   查看 j 对应值 != j-1 对应值,打包前面(count: 3) + previous.charAt(j-1) = '3' + '1' =  '31', count初始化为1

j                          查看 j 对应值 == j-1 对应值, count更新为2

j                   查看 j 对应值 != j-1 对应值,打包前面(count: 2) + previous.charAt(j-1) = '2' + '2' =  '22', count初始化为1

代码:

 class Solution {
public String countAndSay(int n) {
String pre = "1";
for(int i = 2; i<=n; i++){
StringBuilder sb = new StringBuilder();
int count = 1;
for(int j =1; j< pre.length() ; j++){
if(pre.charAt(j) == pre. charAt(j-1)){
count++;
}else{
sb.append(count+"");
sb.append(pre.charAt(j-1));
count = 1;
}
}
sb.append(count+"");
sb.append(pre.charAt(pre.length()-1));
pre = sb.toString(); }
return pre;
}
}

[leetcode]38. Count and Say数数的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. Java [leetcode 38]Count and Say

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

  3. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  4. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  5. [LeetCode] 38. Count and Say_Easy

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  6. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  7. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  8. LeetCode - 38. Count and Say(36ms)

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  9. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

随机推荐

  1. (思维导图搞定)Content-Type:application/json,后台如何接收

    自己定的规范:只要Content-Type设置为application/json的时候,前台的data要传递字符串 虽然设置为application/json,前台传对象request.getPara ...

  2. kafka原理和实践(四)spring-kafka消费者源码

    系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...

  3. Windows安装配置Anaconda2/PyCharm

    一.安装Anaconda2 1.进入Anaconda官网:https://www.anaconda.com/download/,下载对应版本的安装包. 2.下载成功后,打开可执行文件进行安装. 3.N ...

  4. 创建一个HTTP接口测试

    jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具. Apache jmeter 可以用于对静态的和动态的资源(文件,Servlet,Perl脚本,java 对 ...

  5. 石板地面 Base Shape

    软件:Substance Designer 2017.1.2 石板地面 Base Shape 效果见图一 图一:Base Shape (2D View) 首先使用Cells 1(Pattern)结点生 ...

  6. [SQL]T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

    T-Sql 递归查询(给定节点查所有父节点.所有子节点的方法)   -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ ...

  7. centos 设置中文环境

    方法1: [hl@localhost ~]$ LANG=zh_CN.UTF-8 #只对当前shell有效,临时设置 [hl@localhost ~]$ ll 总用量 drwxrwxr-x. hl hl ...

  8. Centos6搭建Samba服务并使用Windows挂载

    一.安装相关软件 [root@mail ~]# yum install samba samba-client -y #安装相关软件 二.配置匿名访问 [root@mail ~]# cd /etc/sa ...

  9. docker上搭建consul集群全流程

    consul简介: consul是提供服务发现.简单配置管理.分区部署的服务注册发现解决方案.主要特性:服务发现\健康检查\基于Key-Value的配置\支持TLS安全通讯\支持多数据中心部署 con ...

  10. Sklearn (一) 监督学习

    本系列博文是根据SKlearn的一个学习小结,并非原创!  1.直接学习TensorFlow有点不知所措,感觉需要一些基础知识做铺垫.  2.之前机器学习都是理论<Ng机器学习基础>+底层 ...