Leetcode_38_count-and-say
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41257397
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.
Note: The sequence of integers will be represented as a string.
今天OJ可好几次,提交好几次都出错,最后才发现是自己把题目理解错了,回头想想就觉得好笑。
题目的意思是给定一个整数n,让你求出按照上面规律在执行若干次后所得到的串,其实该算法主要用到递归的思想。
例如n=1时输出“1”,n=2时输出“2”......
我却把题目意思错误地理解为:对于给定的整数n,对该整数n执行N次上述递归操作后得到的串。例如给定2,得到的结果是1112。
当我将给定整数设定为1000时,果断出现内存泄露,想想就觉得可怕。
按照:“对于给定的整数n,对该整数n执行N次上述递归操作后得到的串”的算法描述如下所示:
public class TestCountAndSay {
private static String countAndSay;
public static void main(String[] args) {
countAndSay = countAndSay(12);
System.err.println(countAndSay);
}
public static String countAndSay(int n) {
String value = String.valueOf(n);
for (int i = 0; i < n; i++) {
value = getAllSays(value);
}
return value;
}
public static String getAllSays(String value){
StringBuffer buffer = new StringBuffer();
int len = value.length();
int pos = 0;
int max = 1;
for (int i = 1; i <=len;i++ ) {
if(i<len && (int)value.charAt(i) == (int)value.charAt(pos)){
max++;
continue;
}else{
buffer.append(String.valueOf(max));
buffer.append(value.charAt(pos));
pos = i;
max = 1;
}
}
return buffer.toString();
}
}
题目真正的解法如下所示:
public static String countAndSay(int n) {
if (n == 1) return "1";
String s = "1";
StringBuffer buffer = new StringBuffer();
//记录重复的值
int count = 0;
// 迭代次数
int round = 0;
int i;
while (++round < n) {
count = 1;
buffer.setLength(0);
for (i = 1; i < s.length(); i++) {
// 重复的值,继续计数
if (s.charAt(i) == s.charAt(i - 1)) {
count++;
} else {
// 有新的值出现,记录到buffer
buffer.append(count).append(s.charAt(i - 1));
// 重置count
count = 1;
}
}
buffer.append(count).append(s.charAt(i - 1));
// 更新s
s = buffer.toString();
}
return buffer.toString();
}
随机推荐
- hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数
如何快速查找一个字符串中出现最多的字符,并统计出现的次数? 可以使用hash数组,也就是关联数组实现快速查找功能. function seek(str) { var hash = []; var ma ...
- 31. Next Permutation(中等,搞清楚啥是 next permutation)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Luogu P1226 取余运算||快速幂_快速幂
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...
- Hive中yyyymmdd和yyyy-mm-dd日期之间的切换
以2017-12-05和20171205相互转换为例说明 方法1: from_unixtime+ unix_timestamp --20171205转成2017-12-05 ','yyyymmdd') ...
- Tomcat安装及问题排查方法
简介: Apache Jakarta的开源项目 JSP/Servlet容器 安装: 1.1进入 Tomcat 官方下载地址 选择合适版本下载,并解压到本地. (备注)Tomcat 8.5 要求 JDK ...
- 前端技术之_CSS详解第一天
前端技术之_CSS详解第一天 一html部分 略.... 二.列表 列表有3种 2.1 无序列表 无序列表,用来表示一个列表的语义,并且每个项目和每个项目之间,是不分先后的. ul就是英语unorde ...
- ELK学习记录一 :初识ELK
ELK是elastic公司提供的一套完整的收集日志并分析展示的产品,分别表示Elasticsearch.Logstash和kibana. (官网截个图) 先来一段个人粗浅的认识: Elasticsea ...
- 解决使用BottomSheetDialog时状态栏变黑的问题
问题描述 当使用support里的design 库里的BottomSheetDialog时,在6.0的机器上当对话框弹出时系统状态栏会变黑,如下图所示: 一开始以为是我用的姿势不对,试过对style配 ...
- Kafka系列之-Kafka Protocol实例分析
本文基于A Guide To The Kafka Protocol文档,以及Spark Streaming中实现的org.apache.spark.streaming.kafka.KafkaClust ...
- Android TV开发总结(六)构建一个TV app的直播节目实例
请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...