[抄题]:

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. 关于C#引用dll动态链接库文件的注释问题

    1.dll动态库文件项目生成属性中要勾选"XML文档文件" 注意:1).要选中项目,查看项目属性,选中解决方案是找不到的.2).XML文件的名字不要修改. 2.添加引用时XML文件 ...

  2. 程序级的AOP到底好不好?

    很多年前模拟过Spring的AOP机制,简单的实现其实不难,但真正要保证切入代码符合预期的设计,不会引起负面影响,特别是要保证原来逻辑的稳定性,即AOP的强壮性.个人感觉还是很难,如果横切的代码过多, ...

  3. Yii 入门

    跳转到不同module Redirect to module after login Yii $this->redirect( array('/tradesman/default/index') ...

  4. Buildroot MariaDB替代MySQL

    /********************************************************************************* * Buildroot Maria ...

  5. python3 openpyxl基本操作

    #coding:utf-8 import xlrd import xlwt # 读写2007 excel import openpyxl import sys #读取设备sn # def readSN ...

  6. 【java基础】java字符串之StringBuffer和StringBuilder

    [一]简述区别 package com.sxf.test.string; public class StringBufferStringBuilderTest { public static void ...

  7. CH1809 匹配统计

    题意 描述 阿轩在纸上写了两个字符串,分别记为A和B.利用在数据结构与算法课上学到的知识,他很容易地求出了"字符串A从任意位置开始的后缀子串"与"字符串B"匹配 ...

  8. BNF 和 ABNF 扩充巴科斯范式 了解

    BNF 巴科斯范式(BNF: Backus-Naur Form 的缩写)是由 John Backus 和 Peter Naur 首先引入的用来描述计算机语言语法的符号集.现在,几乎每一位新编程语言书籍 ...

  9. 阿里云 搭建Git服务器

    1. 服务端安装git, ssh, 客户端安装git sudo apt-get install git sudo apt-get install ssh 2. 服务单创建用户server sudo a ...

  10. C#多线程编程之:异步事件调用

    当一个事件被触发时,订阅该事件的方法将在触发该事件的线程中执行.也就是说,订阅该事件的方法在触发事件的线程中同步执行.由此,存在一个问 题:如果订阅事件的方法执行时间很长,触发事件的线程被阻塞,长时间 ...