Leetcode Count and Say 数数列数字

原创 2013年12月03日 08:33:00

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.

 这个是facebook的面试题,题目不好理解。

这样说会比较清楚:

其实就是一个数列n=1的时候数列为1; n=2,数列为11,n=3数列为21;n=4数列为1211;n=5数列是111221

n=2的时候数数列1有什么数字; n=3的时候数数列2有什么数字; n=4的时候数数列3有什么数字……

我的解法,利用两个临时string,数一个string存入另外一个,如此反复。

class Solution {
public:
	string countAndSay(int n) 
	{
		if (n == 0) return "";
		string str = "1";
		for (int i = 1; i < n; i++)
		{
			char ch = '0';
			string str2 = "";
			int counting = 0;
			for (int j = 0; j < str.length(); j++)
			{
				if (str[j] == ch) counting++;
				else
				{
					if (counting > 0)
						str2 = str2 + char(counting + '0') +ch;
					counting = 1;
					ch = str[j];
				}
			}
			str2 = str2 + char(counting + '0') +ch;
			str = str2;
		}
		return str;
	}
};


 下面是leetcode上比较不错的算法:

http://discuss.leetcode.com/questions/217/count-and-say

class Solution {
public:
	string getNext(string &s) 
	{
		if(s == "") return "1";
		string temp = "";
		for(int i = 0; i < s.size(); i++) {
			int cnt = 1;
			while(i+1 < s.size() && s[i] == s[i+1]) {
				i++;
				cnt++;
			}
			stringstream ss;
			ss << cnt;
			temp += ss.str();
			temp += s[i];
		}
		return temp;
	}
	string countAndSay(int n) 
	{
		string s = "";
		if(n == 0) return s;

		for(int i = 0; i < n; i++) {
			s = getNext(s);            
		}           
		return s;
	}
};


 

 更新下面的程序更加清晰快速点,4ms

//2014-1-26
class Solution {
public:
	string countAndSay(int n) 
	{
		string rs;
		string tmp;
		if (n==0) return rs;
		rs.push_back('1');

		for (int i = 1; i < n; i++)
		{
			int c = 1;//注意:牢记每次都需要重置的时候一定要重置,否则答案错误!!!
			for (int j = 1; j < rs.length(); j++)
			{
				if (rs[j] == rs[j-1]) c++;
				else
				{
					tmp.push_back(c+'0');
					tmp.push_back(rs[j-1]);
					c = 1;
				}
			}
			tmp.push_back(c+'0');
			tmp.push_back(rs.back());
			rs.clear();
			rs.swap(tmp);
		}
		return rs;
	}
};

 

 

版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者允许不得转载。

LeetCode之count-and-say sequence

问题描述:/** * The count-and-say sequence is the sequence of integers beginning as follows: * 1, 11, 2...
  • u011521382
  • u011521382
  • 2016年07月22日 14:56
  • 2758

著名的递推查数序列 Count and Say Sequence

题目源自于leetcode。 题目:The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
  • luckyjoy521
  • luckyjoy521
  • 2013年12月06日 20:40
  • 1394

【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】

【038-Count and Say(计数和表述)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  The count-and-say sequence is the se...
  • DERRANTCM
  • DERRANTCM
  • 2015年07月28日 05:59
  • 2288

LeetCode 之动态规划

1. Jump Game Given an array of non-negative integers, you are initially positioned at the first...
  • sbitswc
  • sbitswc
  • 2014年03月07日 12:27
  • 6832

HDU1181 变形课【DFS】

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Sub...
  • tigerisland45
  • tigerisland45
  • 2016年07月18日 23:23
  • 1067

[leetcode]Count and Say (伯爵说 C语言实现)

Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11,...
  • sanmao0816
  • sanmao0816
  • 2015年03月30日 19:09
  • 1138

leetcode笔记--Count and Say

题目:难度(Easy) The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21...
  • u010366748
  • u010366748
  • 2016年02月24日 15:47
  • 239

LeetCode 37 Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11122...
  • mlweixiao
  • mlweixiao
  • 2014年06月18日 14:15
  • 1078

【LEETCODE】38-Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2015年12月23日 19:50
  • 239

Leetcode (3) Count and Say

题目描述The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11...
  • angelazy
  • angelazy
  • 2015年04月11日 15:22
  • 461
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode Count and Say 数数列数字
举报原因:
原因补充:

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