博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Count and Say
阅读量:4071 次
发布时间:2019-05-25

本文共 795 字,大约阅读时间需要 2 分钟。

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.

开始没弄太明白,在找规律,而且以为只能是 1和 2 的组合,终于找到规律了,但却误解了题意,比如连续 4 个 1 会说 4 个 1 而不会说 “2 个 1 ,2 个1”,汗

class Solution {public:    string countAndSay(int n) {    string re("");	if(n <= 0) return re;	re += '1';	string tmp;	for (int i = 2; i <= n; ++i)	{		tmp = re;		re.clear();		for (int j = 0; j < tmp.length();++j)		{		    int cnt = 1;			while(j + 1 < tmp.length() && tmp[j] == tmp[j + 1])			{			    ++j;			    ++cnt;			}			re += (cnt + '0');			re += tmp[j];		}	}	return re;    }};

转载地址:http://orlji.baihongyu.com/

你可能感兴趣的文章
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>