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 数数列数字

这个是facebook的面试题,题目不好理解。 这样说会比较清楚: 其实就是一个数列n=1的时候数列为1; n=2,数列为11,n=3数列为21;n=4数列为1211;n=5数列是111221 ...

[leetcode]38. Count and Say@Java

https://leetcode.com/problems/count-and-say/#/description The count-and-say sequence is t...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

leetcode_Count and Say

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

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
  • 3457

leetcode ——Count and Say

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

LeetCode Count and Say

题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 12...

[LeetCode] Count and Say

count-and-say序列是如上所示的序列,比如,1,可以读作1个1, 所以下一个数就是11,11可以读作2个1,所以,下一个就是21。给定一个整数n,生成序列的第n个整数序列,该整数序列表示为字...

Leetcode: Count and Say

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

《leetCode》:Count and Say

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

Count and Say --leetcode

Count and Say --leetcode 题目如下 The count-and-say sequence is the sequence of in...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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