本文是在学习中的总结,欢迎转载但请注明出处: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. 剑指架构师系列-ftp服务器

    1.安装FTP 我们在开发项目时,肯定需要专门的一台ftp服务器来存在上传的静态资源,今天我们就在CentOS下搭建一个ftp服务器. 1.安装vsftpd组件,安装完后,有/etc/vsftpd/v ...

  2. 牛客网编程练习之PAT乙级(Basic Level):1034 写出这个数

    AC代码: import java.util.*; /** * @author CC11001100 */ public class Main { public static void main(St ...

  3. jQuery 遍历 – 后代

    后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...

  4. U盘PE无人值守安装centOS6

    一.制作 1.需要用到的工具:老毛桃PX工具.系统ISO.一个8GU盘 老毛桃PE工具 http://laomaotao.net/ CentOS启动映像 http://mirrors.163.com/ ...

  5. CommonsChunkPlugin相关

    参考https://webpack.js.org/guides/caching/#deterministic-hashes var path = require("path"); ...

  6. javaweb异常提示信息统一处理(使用springmvc,附源码)

    一.前言 后台出现异常如何友好而又高效地回显到前端呢?直接将一堆的错误信息抛给用户界面,显然不合适. 先不考虑代码实现,我们希望是这样的: (1)如果是页面跳转的请求,出现异常了,我们希望跳转到一个异 ...

  7. 全废话SQL Server统计信息(1)——统计信息简介

    当心空无一物,它便无边无涯.树在.山在.大地在.岁月在.我在.你还要怎样更好的世界?--张晓风<我在> 为什么要写这个内容? 随着工作经历的积累,越来越感觉到,大量的关系型数据库的性能问题 ...

  8. Chrome 内存和CPU消耗量双料冠军

    今天统计了下某个时刻各进程的内存和CPU使用概况.结果发现,Chrome消耗量真是不一般的大.比Windows主进程都还猛! 另外发现百度安全卫士占用CPU也比较猛. powershell下输入: p ...

  9. OpenMP实现生产者消费者模型

    生产者消费者模型已经很古老了吧,最近写了个OpenMP版的此模型之实现,来分享下. 先说一下模型的大致做法是: 1.生产者需要取任务,生产产品. 2.消费者需要取产品,消费产品. 生产者在生产某个产品 ...

  10. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...