[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么初始化oldstring: 设置成第一个字符串“1”即可, 初始化数字个数也是1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

sb.append(里面是字符串而不是sb,用来延长字符串)

[一句话思路]:

当前的字符串都用oldstring来维护

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 求前i个数,直接用--n先减再给的情况,写起来比for更方便

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

初始化oldstring: 设置成第一个字符串“1”即可, 初始化数字个数也是1

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

public class Solution {
/**
* @param n: the nth
* @return: the nth sequence
*/
public String countAndSay(int n) {
// write your code here
//cc
if (n <= 0) return "";
String oldString = "1"; //while loop
while (--n > 0) {
char[] oldChars = oldString.toCharArray();
StringBuilder sb = new StringBuilder(); for (int i = 0; i < oldChars.length; i++) {
int count = 1;
while ((i + 1) < oldChars.length &&
oldChars[i] == oldChars[i + 1]) {
count++;
i++;
}
sb.append(String.valueOf(count) + String.valueOf(oldChars[i]));
}
oldString = sb.toString();
} return oldString;
}
}

38.Count and Say 报数的更多相关文章

  1. LeetCode - 38. Count and Say

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

  2. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

  3. leetCode练题——38. Count and Say

    1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...

  4. 【LeetCode】Count and Say(报数)

    这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...

  5. lintcode :Count and Say 报数

    题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...

  6. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  7. 38. Count and Say

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

  8. 【LeetCode】38 - Count and Say

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

  9. Java [leetcode 38]Count and Say

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

随机推荐

  1. protel 99se 加载库文件 files not recognised 解决办法-转

    WIN7操作系统下,protel99se添加元件库的操作方法(非修改ADVSch99SE方法) 最近更换了新电脑,操作系统是正版的WIN7,在用protel时发现元件库无法加载,很是郁闷,上网查找解决 ...

  2. cuDnn的安装ubuntu16.04环境下(tensorflow正式安装之前的必备安装操作)

    首先,下载cuDnn,地址如下: https://developer.nvidia.com/rdp/cudnn-archive 本人下载,Linux版本: 解压: tar -zxvf cudnn-8. ...

  3. numpy安装包scipy

    https://sourceforge.net/projects/scipy/files/scipy/0.11.0/

  4. office web app server 文件预览部署&& wopi 集成使用

    对于需要进行office 套件文档预览的时候大部分大家使用的是插件,或者类似的,解决方案,微软已经为我们提供了比较好的解决 方案 office web app server (目前名称是office ...

  5. 怎样创造財富?硅谷创业之父 Paul Graham 《黑客与画家》思维导图

    先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 下载大图:http://caifujianghu.com/article/ruhe-c ...

  6. WinSCP一个好用的连接linux服务器的

    用虚拟机ssh登陆远程服务器,终端命令copy本地文件到服务器简直弱爆了. 不然用win下的WinSCP,牛逼到爆了.操作跟FTP软件差不多

  7. linux之 LVM扩容

    1. 查看本机现在磁盘的情况[root@oralce10g ~]# df Filesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/ ...

  8. CentOS7.2部署FTP

    目前Linux大部分部署的FTP服务器都是vsftpd,至于为什么,暂时没什么必要深究. 1.安装vsftpd # yum check-update //检查可更新的程序,也可以不更新直接安装,以防万 ...

  9. 【python】smtp邮件发送

    纯文本: #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from ...

  10. codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)

    B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...