Leetcode 38,Count and say

转载 2016年05月31日 14:01:10
//思路:两重循环,第一重循环循环个数,第二重循环判断已生成字符,或者增加数量,或者把数字和字符加上。
//答案用这个http://huntfor.iteye.com/blog/2059877(以下只是把ij换了下位置)。
public static String countAndSay(int n) {
	if (n <= 0) {
		return null;
	}
	String str = "1";
	int num = 1;
	for (int i = 0; i < n - 1; i++) {
		StringBuilder sb = new StringBuilder();
		//generate countAndSay based on sb
		for (int j = 0; j < str.length(); j++) {
			if (j + 1 < str.length() && str.charAt(j) == str.charAt(j + 1)) {
				num++;
			} else {
				sb.append(num + "" + str.charAt(j));
				num = 1;
			}
		}
		str = sb.toString();
	}
	return str;
}

相关文章推荐

【LEETCODE】38-Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...

leetcode38 Count and Say

package leetcode38; import java.util.ArrayList; /** * The count-and-say sequence is the sequence ...

LeetCode 38 Count and Say(计数与报数)

翻译计数报数序列按如下规律开始递增: 1,11,21,1211,111221,……1 读作“1个1”或11. 11 读作“2个1”或21. 21 读作“1个2,1个1”或1211.给定一个整数n,生成...
  • NoMasp
  • NoMasp
  • 2015年12月03日 10:32
  • 3852

LeetCode 38 Count and Say(字符串生成)

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. ...

【leetcode c++】38 Count and Say

Count and Say The count-and-say sequence is the sequenceof integers beginning as follows: 1, 11, 2...
  • hqq39
  • hqq39
  • 2015年06月26日 22:40
  • 347

Leetcode - 38. Count and Say

38. Count and Say题目简介 The count-and-say sequence is the sequence of integers beginning as follows:...

leetcode 38:count and say

问题描述:The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...

[leetcode-38]Count and Say

自动生成序列的一道题,想起来昨天比赛里遇见的另一道题,问C_∞序列第N位是多少,昨天死磕到最后也就是写了个垃圾代码,然后输入N>10就不能运行,觉得这类题的要点是要开两个序列,一个生成另一个后,然后拷...

<LeetCode OJ> 38. Count and Say(测试案例或有错)

38. Count and Say My Submissions Question Total Accepted: 66138 Total Submissions: 243196 Diffic...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode 38,Count and say
举报原因:
原因补充:

(最多只允许输入30个字)