38.Count and Say 报数
[抄题]:
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来维护
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 求前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 报数的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
- 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 ...
- 【LeetCode】Count and Say(报数)
这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...
- lintcode :Count and Say 报数
题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...
- 【leetcode❤python】 38. Count and Say
#-*- coding: UTF-8 -*- class Solution(object): def countAndSay(self, n): """ ...
- 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
随机推荐
- Centos7 安装JDK环境和Tomcat
Linux JDK 64位下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.ht ...
- C++可调用对象与函数表
c++的可调用对象 有 函数 函数指针 lambda表达式 bind的对象 重载了函数调用运算符的类 如何调用? 函数调用 void afuncToCall() { cout << &qu ...
- Linux运维学习笔记-软硬链接知识总结
文件链接 硬链接,通过索引节点来进行链接 硬链接原理图 硬链接的创建: 直接执行命令“ln 源文件 硬链接文件”,即可完成创建硬链接. 硬链接知识小结: 1.具有相同Inode节点号的多个文件是互 ...
- Word操作(基于word2013)【非编程类】
一.生成目录 1.word支持自动生成目录功能,生成地点为操作时光标的落点 2.生成方式:打开首选项,进入引用标签,如下图所示最左边即为目录选项. 一般有3个内置目录类型 a:手动目录,生成目录后只有 ...
- guake terminal
类是gnome的终端,超级棒. F11可以全屏/半屏,F12可以显示/隐藏.右键--首选项可以设置配置信息.
- SQL Server2008 R2开启远程连接总结
============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...
- 使用nomad && consul && fabio 创建简单的微服务系统
具体每个组件的功能就不详细说明了 nomad 一个调度工具,consul 一个服务发现,健康检查多数据中心支持的工具 fabio 一个基于consul的负载均衡&&动态路由工具,对于集 ...
- php in_array的坑
今天做一个根据用户充值领礼包码的活动,遇到一个问题部分用户领礼包时会一直提示“系统错误,请稍后再试”,这是什么情况,一开始以为接口出错了,一番排查后发现了问题所在,是in_array坑了~~~ 情况大 ...
- Web自动化测试框架Watir(基于Ruby) - 第1章 Windows下安装与部署
一.前言 Web自动化测试一直是一个比较迫切的问题,对于现在web开发的敏捷开发,却没有相对应的敏捷测试,故开此主题,一边研究,一边将Web自动化测试应用于工作中,进而形成能够独立成章的博文,希望能够 ...
- vim配置之安装脚本
vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...