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. vim 简单实用

    http://www.runoob.com/linux/linux-vim.html 编辑模式 : (同时打开两个文件) vim test.c test1.c -O     同时编辑两个文件    - ...

  2. 引入public文件目錄下js/css文件

    <link href="{{ URL::asset('css/ySelect.css') }}" rel="stylesheet" type=" ...

  3. py-day3-2 python 函数递归

    # 递归 def calc(n): print(n) if int(n/2) == 0: return n res = calc(int(n/2)) return res res = calc(10) ...

  4. 【转】利用Boost.Python将C++代码封装为Python模块

    用Boost.Python将C++代码封装为Python模块 一.     基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...

  5. 【java】static用法

    static作用: 用来修饰函数成员,成员变量和成员函数.类对象的属性都一致且能共享,比如国籍,这就能用static修饰,name不能共享,因为每个人都有自己的名字. 特有内容(name)随着对象存储 ...

  6. python永久添加环境变量

    import sys sys.path 系统环境是一个list,可以将自己需要的库添加进入,例如mysql库,hive库等等.有三种方式添加,均验证通过:     1 临时添加,在一个shell窗口中 ...

  7. 物化视图SQL

    物化视图SQL,如果没有结果集,就证明数据库中不存在物化视图 select a.owner,'' column_name,a.table_name,b.segment_name,b.segment_t ...

  8. HTML/CSS基础知识(三)

    浏览器内核 浏览器内核是浏览器的核心,也称“渲染引擎”,用来解释网页语法并渲染到网页上.浏览器内核决定了浏览器该如何显示网页内容以及页面的格式信息.不同的浏览器内核对网页的语法解释也不同,因此网页开发 ...

  9. k8s学习笔记之八:存储卷

    第一章.前言 默认情况下容器的数据都是非持久化的, 在容器消亡以后数据也跟着丢失, 所以 Docker 提供了 Volume 机制以便将数据持久化存储. 类似的, Kubernetes 提供了更强大的 ...

  10. debug protractor

    HTAir:protractor-cucumber-typescript kbladewht$ node --inspect-brk=0.0.0.0:1229 node_modules/protrac ...