本文是在学习中的总结,欢迎转载但请注明出处: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. EM vs REM vs PX,为什么你不应该”只用px“”

    Actually this artical is from other person's opnion ,i just put it into chinese,and this means a ver ...

  2. Java锁Synchronized对象锁和类锁区别

    java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁.获得内置锁的唯一途径就是进入这个锁的保 ...

  3. python字典无限遍历

    #无限遍历dict,通过key获取value,嵌套字典存在多个相同的key,可获取多个key class getValues(object): def __init__(self): pass #无限 ...

  4. Java不走弯路教程(3.用户验证与文件内容查询)

    3.用户验证与文件内容查询 在上一章中,我们完成了对指定文件内容的输出操作. 我们现在有如下格式的文件product.db id,product_name,product_detail 1,noteb ...

  5. ACM Max Factor

    To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) co ...

  6. PHP MySQL Delete

    DELETE 语句用于从数据库表中删除行. 删除数据库中的数据 DELETE FROM 语句用于从数据库表中删除记录. 语法 DELETE FROM table_name WHERE some_col ...

  7. Java中Semaphore(信号量)的使用

    Semaphore的作用: 在java中,使用了synchronized关键字和Lock锁实现了资源的并发访问控制,在同一时间只允许唯一了线程进入临界区访问资源(读锁除外),这样子控制的主要目的是为了 ...

  8. hive分区partition(动态和静态分区混合使用; partition的简介)

    分区是hive存放数据的一种方式.将列值作为目录来存放数据,就是一个分区.这样where中给出列值时,只需根据列值直接扫描对应目录下的数据,不扫面其他不关心的分区,快速定位,查询节省大量时间.分动态和 ...

  9. Leetcode解题-链表(2.2.2)ReverseLinkedList

    题目:2.2.2 Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in on ...

  10. FORM实现中打开图片,链接,文档(参考自itpub上一篇帖子,整理而来)

    FORM实现中打开图片,链接,文档 参考自itpub上一篇帖子,整理而来 1.添加PL程序库D2kwutil.pll 2.主要实现程序 /*过程参数说明: v_application --打开文件的应 ...