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. PythonStudy——字符串扩展方法 String extension method

    ')) ')) print('***000123123***'.lstrip('*')) print('***000123123***'.rstrip('*')) print('华丽分割线'.cent ...

  2. telnet-redis-quit

    很多时候 telnet 完就无法退出了,ctrl+c 有时也无法退出后来找到了正确的命令 ctrl+] 然后在telnet 命令行输入 quit 就可以退出了 telnet Trying 10.168 ...

  3. linux下insmod模块出现“Invalid parameters"

    在编译一个模块时,会出现WARNING:"函数名" undefined!,这 说明该模块所依赖的模块还没有加载进内核,需要先加载所依赖的模块. 当加载依赖模块后,使用insmod会 ...

  4. SQL: 某个时间段范围内,产品有价格,且求平均数

    select ID,AVG(fPrice) as avgPrice from Price where Hp_Date >='2017-07-04' and Hp_Date <='2017- ...

  5. 一次Windows 安装问题

    在 thinpad x250上安装 windows 10时,提示"安装程序无法创建新的系统分区,也无法定位现有系统分区". x250 的主板上自带一个16G的闪存且标识为 主分区, ...

  6. lumbda表达式初探

    一.表达式格式定义 (parameters) -> expression 或 (parameters) ->{ statements; } 注意点:左边是输入参数,就相当于我们定义方法中的 ...

  7. Excel清除无用数据行和数据列

    http://jingyan.baidu.com/article/6525d4b13ae608ac7c2e9478.html ctrl+shift+↓ ctrl+- ctrl+shift+→ ctrl ...

  8. JavaWeb——tomcat manager 403 Access Denied .You are not authorized to view this page.

    403 Access Denied You are not authorized to view this page. If you have already configured the Manag ...

  9. sysctl-p报错:error: "net.bridge.bridge-nf-call-ip6ta

    1.刚配置完sysctl,加载时报错:[root@itpux1 yum.repos.d]# sysctl -pnet.ipv4.ip_forward = 0net.ipv4.conf.default. ...

  10. BBS(第一天)项目之 注册功能实现通过forms验证与 前端ajax请求触发查询数据库判断用户是否存在的功能实现

    1.BBS项目之注册功能通过forms验证 from django import forms from blog.models import User from django.contrib.auth ...