乘风破浪:LeetCode真题_038_Count and Say
乘风破浪:LeetCode真题_038_Count and Say
一、前言
这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势。
二、Count and Say
2.1 问题


2.2 分析与解决
这道题最难的其实是理解,因为描述的不尽其意,所以很难理解,其实就是根据最开始的字符串,不断的扩展,输入的N,代表的是经过N次之后的结果,与其中的结果没有任何关系。
/**
* 题目大意
* n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,
* 所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,
* 有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。
*
* 解题思路
* 第一种情况:n<0时返回null。
* 第二种情况:当n=1时,返回1
* 第三种情况:当n>1时,假设n-1返回的字符串是s,对s的串进行处理,对不同的数字
* 进行分组比如112365477899,分成11,2,3,6,5,4,77,8,99。最有就2个1,
* 1个2,1个3,1个6,1个5,一个4,2个7,1个8,2个9,就是211213161614271829,返回此结果。
*/
理解题意之后,其他的就好办了:
public class Solution {
public String countAndSay(int n) {
if (n < 1) {
return null;
}
String result = "1";
for (int i = 2; i <= n; i++) {
result = countAndSay(result);
}
return result;
}
public String countAndSay(String str) {
StringBuilder builder = new StringBuilder(128);
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == str.charAt(i - 1)) {
count++;
} else {
builder.append(count);
builder.append(str.charAt(i - 1));
count = 1;
}
}
builder.append(count);
builder.append(str.charAt(str.length() - 1));
return builder.toString();
}
}
当然也可以用递归来做,其实道理是一样的,速度稍微慢一点:
public class Solution {
public String countAndSay(int n) {
if(n == 1) { return "1"; }
String s = countAndSay(n-1);
int i = 0;
String out = "";
while(i < s.length()) {
int j = i+1;
while(j < s.length() && s.charAt(i) == s.charAt(j)) {
j += 1;
}
out = out + (j-i) + s.charAt(i);
i = j;
}
return out;
}
}

三、总结
题目的理解非常重要,有的时候靠自己的猜测是不正确的。
乘风破浪:LeetCode真题_038_Count and Say的更多相关文章
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_037_Sudoku Solver
乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决 这道题 ...
- 乘风破浪:LeetCode真题_036_Valid Sudoku
乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array
乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言 将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
随机推荐
- Deep learning with Python 学习笔记(3)
本节介绍基于Keras的使用预训练模型方法 想要将深度学习应用于小型图像数据集,一种常用且非常高效的方法是使用预训练网络.预训练网络(pretrained network)是一个保存好的网络,之前已在 ...
- 用python写web一定要去破解的异步请求问题.经历web.py和tornado,完破!
1.问题 上个学期,给学校写了一个数据服务,主要从oracle里面读取一些数据供查询使用,非常快速的用web.py搭建了起来.调试顺利,测试正常,上线!接下来就是挨骂了,我铁定知道会卡,但是没想到会那 ...
- C#使用命令编译代码
1.在路径%SystemRoot%\Microsoft.NET\Framework\vX.X.X(安装的.net版本号)下找到csc.exe,在cmd窗口cd到该路径下. ps(在该路径下有一个CSC ...
- eclipse中Cannot change version of project facet Dynamic Web Module to 3.0的问题解决
在做web配置的时候,希望将web Module(Web模块)更换为3.0,发生如下错误: cannot change version of project facet Dynamic Web Mod ...
- 撩课-Web大前端每天5道面试题-Day27
1.浏览器缓存? 浏览器缓存分为强缓存和协商缓存.当客户端请求某个资源时,获取缓存的流程如下: 先根据这个资源的一些 http header 判断它是否命中强缓存, 如果命中,则直接从本地获取缓存资源 ...
- Elasticsearch Query DSL 整理总结(三)—— Match Phrase Query 和 Match Phrase Prefix Query
目录 引言 Match Phase Query slop 参数 analyzer 参数 zero terms query Match Phrase 前缀查询 max_expansions 小结 参考文 ...
- php命令行生成项目结构
ghostinit.php <?php class ghostinit{ static $v = 'ghost version is 1.1'; static function init(){ ...
- jQuery获取子元素的个数
一.获取div下的子元素的个数 $("div").children().length; 二.获取div下的span子元素的个数 $("div").childre ...
- influxdb-1.7.2.x86_64安装 install influxdb-1.7.2.x86_64 on RedHat & CentOS
1.下载安装 wget http://dl.influxdata.com/influxdb/releases/influxdb-1.7.2.x86_64.rpm https://portal.infl ...
- 洛谷P2045 方格取数加强版(费用流)
题意 题目链接 Sol 这题能想到费用流就不难做了 从S向(1, 1)连费用为0,流量为K的边 从(n, n)向T连费用为0,流量为K的边 对于每个点我们可以拆点限流,同时为了保证每个点只被经过一次, ...