【LeetCode】38 - 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.
Solution:下一个字符串= 当前字符串:∑(字符个数+字符),例如1211,下一个字符串=count(1)+'1'+count(2)+'2'+count(1)+'1'=111221
class Solution {
public:
string countAndSay(int n) {
string ret="";
if(n==)return ret;
while(--n){
string next;
char last=ret[];
int i=,count=;
while(i<ret.size())
{
while(ret[i]==last&&i<ret.size()){
i++;
count++;
}
if(i<ret.size()&&ret[i]!=last){
next+=to_string(count);
next+=last;
count=;
last=ret[i];
i++;
}
}
next+=to_string(count);
next+=last;
ret=next;
}
return ret;
}
};
【LeetCode】38 - Count and Say的更多相关文章
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- 【LeetCode】38. 外观数列 Count and Say
作者: 负雪明烛 id: fuxuemingzhu 公众号:负雪明烛 本文关键词:LeetCode,力扣,算法,算法题,外观数列,Count and Say,刷题群 目录 题目描述 题目大意 解题方法 ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- 【Leetcode】357. Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
随机推荐
- Linux远程文件传输
linux系统中,难免会遇到一些要将某文件通过网络传送给其他主机的情况,而恰好两台主机 都是linux系统的时候,我们就可以直接使用scp命令来传输文件到另一台主机了. scp命令用于在网络中安全的传 ...
- Linux命令-grep
grep命令用于对文本进行搜索,格式为“grep [选项] [文件]” 搜索某个关键词:"grep 关键词 文本文件" 参数说明 -b 将可执行文件当做文本文件来搜索 -c 仅显示 ...
- SparkContext和RDD
SparkContext.scala实现了一个SparkContext的class和object,SparkContext类似Spark的入口,负责连接Spark集群,创建RDD,累积量和广播量等. ...
- c# 任意多个数,求最大值
c# 任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!
- 标准类型内建函数 cmp()介绍
内建函数cmp()用于比较两个对象obj1 和obj2, 如果obj1 小于obj2, 则返回一个负整数,如果obj1 大于obj2 则返回一个正整数, 如果obj1 等于obj2, 则返回0.它的行 ...
- 运行所有sdk目录下的示例,查看它们的功能,方便以后查寻
运行所有sdk目录下的示例,查看它们的功能,方便以后查寻
- Oracle中用一个表的数据更新另一个表的数据
update tbl1 a set (a.col1, a.col2) = (select b.col1, b.col2 from tbl2 ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- spring读写分离(配置多数据源)[marked]
我们今天的主角是AbstractRoutingDataSource,在Spring2.0.1发布之后,引入了AbstractRoutingDataSource,使用该类可以实现普遍意义上的多数据源管理 ...
- 【转载】Javascript中的this关键字
看了这篇文章 http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html 分情况讨论. 情况一:纯粹的函数 ...