本文是在学习中的总结,欢迎转载但请注明出处: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();
	}

随机推荐

  1. svg和css实现波浪动效

    效果: 截图有点模糊~ 实现: <svg教程> //html <body> <svg class="wave-container" xmlns=&qu ...

  2. Response ServletContext 中文乱码 Request 编码 请求行 共享数据 转发重定向

    Day35  Response 1.1.1 ServletContext概念 u 项目的管理者(上下文对象),服务器启动时,会为每一个项目创建一个对应的ServletContext对象. 1.1.2  ...

  3. 0426html常用标签属性

    一.基础语法 标签:作为网页的最小单元 1.双标签 内容的容器 2.单标签 控制性内容 注释    每一个模块都要写清楚注释 二.基本结构 <!DOCTYPE html>          ...

  4. hiredis的各种windows版本

    hiredis的各种windows版本(金庆的专栏 2016.12)hiredis 是内存数据库 redis 的客户端C库, 不支持Windows.hiredis的Windows移植版本有许多:des ...

  5. Unable to ignore resources

    摘要:分享牛,分享牛系列, Unable to ignore resources Attempted to beginRule: 异常信息处理. 出现Unable to ignore resource ...

  6. 十六进制字符串转化为十进制值strtoul函数

    eg: NSString *strtest =@"7fffffff"; NSUInteger val = strtoul([[strtest substringWithRange: ...

  7. Dynamics CRM2013 从外部系统取到CRM系统的用户头像

    CRM从2013开始引入了entityimage的概念,具体这个字段怎么设置的,图像是怎么上传的这里就不谈了.说实在的这玩意在项目中没啥用,所以也没去关注,直到最近遇到了个难题,要在外部系统去获取这个 ...

  8. 一张图带你看懂SpriteKit中Update Loop究竟做了神马!

    1首先Scene中只有开始一点时间用来回调其中的update方法 ;] 2然后是Scene中所有动作的模拟 3接下来是上一步完成之后,给你一个机会执行一些代码 4然后是Scene模拟其中的物理世界 5 ...

  9. [Mysql]mysql windows下配置文件

    环境是win7 mysql5.6版本 测试下配置文件是否可用(之前没用过windows下的msyql配置) 修改配置前查询下: mysql> show variables like '%max_ ...

  10. 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)

    安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...