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, ...
随机推荐
- springboot---数据整合篇
本文讲解 Spring Boot 基础下,如何使用 JDBC,配置数据源和通过 JdbcTemplate 编写数据访问. 环境依赖 修改 POM 文件,添加spring-boot-starter-jd ...
- 5.2离线使用xadmin包
把xadmin包放到项目目录下,便于修改xadmin中的代码. 首先解压下载好的 xadmin-django2.zip 压缩包,拷贝子文件夹中的xadmin文件夹,到项目中新建extra_apps文件 ...
- MPI2 编程环境搭建 MPI4PY 编程环境搭建
最近发现了一门新语言 Julia , 这门编程语言据说大有取代 Python语言成为数据科学的大佬,但是细看发现最主要说的是这门编程语言运行速度比较快,并且在分布式和并行计算上比较有优势,这时候 ...
- Yii用AJAX注册验证
<script type="text/javascript"> $(document).ready(function(){ $('#RegisterForm_usern ...
- CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)
You are a given a list of integers a 1 ,a 2 ,…,a n a1,a2,…,an and s s of its segments [l j ;r j ] [ ...
- php fwrite写入文件bom头导致的乱码问题解决
最近导出文件遇到fwrite导出乱码,而且中英文都乱码,很费解.折腾了一番之后终于找到问题所在了,mark下. UTF-8 BOM 又叫 UTF-8 签名,其实 UTF-8 的 BOM 对 UFT-8 ...
- Spring 装配Bean入门级
装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean B ...
- (判断)window.open()窗口被关闭后执行事件
$(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...
- PHP框架(如:laravel、yii2、thinkPHP5)中统一异常处理及统一日志打印
背景: 现在写接口服务应用有一个很通用的需求,想通过日志.或者监控的形式监测的接口的运行情况,比如耗时.请求参数.响应结果.和前端联调接口时或者排查线上问题时日志必不可少,特别是现场日志. 应用运行时 ...
- 黄聪:360浏览器、chrome开发扩展插件教程(2)为html添加行为
转载:http://www.cnblogs.com/walkingp/archive/2011/04/02/2002668.html 上一节我们已经讲了Chrome扩展的基础知识,并构建了基础的htm ...