038 - Count and Say

原创 2015年11月19日 17:05:32

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.



数和说,这题目够无聊的

比如1211 我们会说1个1, 1个2, 2个1 既转换车概念111221


char* countAndSay(int n) 
{
	char *ret = (char *)calloc(sizeof(char), 8192);
	char *tmp = (char *)calloc(sizeof(char), 8192);
	char buff[8];
	ret[0] = '1';
	if (n == 1) return ret;
	n--;
	while (n--) {
		char *p = ret;
		char *move;
		char *pp = tmp;
		while (*p) {
			char say = p[0];
			int count = 0;
			move = p;
			while (move[count] == *p) {
				count++;
			}
			snprintf(buff, 8, "%d%c", count, say);
			strcat(tmp, buff);
			p += count;
		}
		memcpy(ret, tmp, strlen(tmp));
		tmp[0] = 0;
	}
	free(tmp);
	return ret;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode038 Count and Say

详细见:leetcode.com/problems/count-and-say Java Solution: github package leetcode; public clas...
  • zxwtry
  • zxwtry
  • 2017年04月05日 15:46
  • 87

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

【038-Count and Say(计数和表述)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  The count-and-say sequence is the se...

leetcode之路038 Count and Say

题目大意:一个count-and-say序列是一个整数的序列,前面的定义如下,给定一个整数n,输出第n个序列。 1,11,21,1211,111221,312211。。。 如何构造这个序列的: 1 i...

Leetcode Count and Say 数数列数字

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

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

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

leetcode笔记--Count and Say

题目:难度(Easy) The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21...

LeetCode 37 Count and Say

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

【LEETCODE】38-Count and Say

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

Leetcode (3) Count and Say

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

leetcode38 Count and Say

package leetcode38; import java.util.ArrayList; /** * The count-and-say sequence is the sequence ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:038 - Count and Say
举报原因:
原因补充:

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