`
473687880
  • 浏览: 482133 次
文章分类
社区版块
存档分类
最新评论

Count and Say @LeetCode

 
阅读更多

题目比较不直观,这里的描述比较好一些 http://www.careercup.com/question?id=4425679

"Count and Say problem" Write a code to do following:
n String to print
0 1
1 1 1
2 2 1
3 1 2 1 1
...
Base case: n = 0 print "1"
for n = 1, look at previous string and write number of times a digit is seen and the digit itself. In this case, digit 1 is seen 1 time in a row... so print "1 1"
for n = 2, digit 1 is seen two times in a row, so print "2 1"
for n = 3, digit 2 is seen 1 time and then digit 1 is seen 1 so print "1 2 1 1"
for n = 4 you will print "1 1 1 2 2 1"

Consider the numbers as integers for simplicity. e.g. if previous string is "10 1" then the next will be "1 10 1 1" and the next one will be "1 1 1 10 2 1"


package Level2;

/**
 * Count and Say
 * 
 *  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.
 *
 */
public class S38 {

	public static void main(String[] args) {
		System.out.println(countAndSay(6));
	}
	
	public static String countAndSay(int n) {
		
		if(n == 1){
			return "1";
		}
		
		String s = "1";
		StringBuffer ret = new StringBuffer();
		int cnt = 0;
		int round = 0;			// round是迭代多少次
		int i;
		while(++round < n){
			cnt = 1;
			ret.setLength(0);
			for(i=1; i<s.length(); i++){
				if(s.charAt(i) == s.charAt(i-1)){		// 重复的值,继续计数
					cnt++;
				}else{			// 有新的值出现,记录到ret
					ret.append(cnt).append(s.charAt(i-1));
					cnt = 1;		// 重置cnt
				}
			}
			ret.append(cnt).append(s.charAt(i-1));
			s = ret.toString();	// 更新s
		}
		return ret.toString();
	}

}


分享到:
评论

相关推荐

    LeetCode原题Count and Say

    Leetcode原题Count and Say count-and-say序列是整数序列,前五个术语如下: 1. 1 2. 11 3. 21 1211 5. 111221 1被读作“1”或11。 11被读作“两个1”或21。 21被读作“一个2,然后一个1”或1211。 给定整数n,...

    LeetCode最全代码

    201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...

    leetcode答案-LeetCode:Swift中的LeetCode

    leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode ...Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L

    LeetCode解题总结

    3.12 Count and Say 3.13 变位词 3.14 简化系统路径 3.15 最后一个单词的长度 3.16 反转字符串中的单词 3.16.1 字符串前后和中间可能存在多个空格 3.16.2 不存在前后和中间的多余空格 3.17 一个编辑距离 4. 栈 4.1 ...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] ...count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) ...Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi

    2sumleetcode-LeetCode:力码

    1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_number.cpp - 3...

    leetcode和oj-OJ_Solution:leetcode、poj等OJ解决方案

    https://leetcode.com/problems/count-and-say/ 3, java/dungenegame https://leetcode.com/problems/dungeon-game/ 4, java/findminrotaed 5, java/houserobber https://leetcode.com/problems/house-robber/

    leetcode给单链表加一js实现-leetcode-By[removed]LeetCode解决方案(全部通过JavaScript!)h

    leetcode给单链表加一js实现 LeetCode By JavaScript LeetCode Solutions (All By JavaScript!) 的个人Solutions汇总,全部使用JavaScript完成:grinning_squinting_face: 目的:了解、掌握数据结构和算法,当然...Say:

    leetcode添加元素使和等于-leetcode:leetcode

    Count_and_Say 循环n次依次计算即可,注意0次等边界情况 Longest_Consecutive_Sequence 双向确认是否连续,避免重复的n^2复杂度 Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前...

    判断链表是否为回文链表leetcode-Algos_Practice:来自Leetcode、HackerRank等网站的练习题

    “count_n_say.py” - 使用迭代方法生成 Count 和 Say 序列的第 n 项。 “count_segments.py” - 该算法计算字符串中的段数,其中段被定义为连续的非空格字符序列。 "count_negatives_2d_array.py" - 给定一个二维...

    lovely-nuts:这是一个可爱的坚果

    Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档.每道下面的题都有详细的解题思路,和知识点分析,尽请参考。 找实习的小伙伴也可以参考我的Tech-blog...038.Count and Say 递归 040.C

    cpp-算法精粹

    Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram ...

Global site tag (gtag.js) - Google Analytics