38. Count and Say (String; DP)
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.
思路:序列问题,用动态规划存储上一个状态,从而能够推导出下一个元素
class Solution {
public:
string countAndSay(int n) {
string previous = "";
string current = "";
string tmp;
stringstream ss;
int times;
for(int i = ; i <= n; i++){ //从头开始推导序列中每个元素
for(int j = ; j < previous.length(); j++){ //遍历前一个元素中的每一位
times = ;
while(j+ < previous.length()&&previous[j+]==previous[j]){
times++;
j++;
}
ss.clear();
ss << times;
ss >> tmp;
current.append(tmp);
ss.clear();
ss << previous[j];
ss >> tmp;
current.append(tmp);
}
previous = current;
current = "";
}
return previous;
}
};
38. Count and Say (String; DP)的更多相关文章
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
- leetCode练题——38. Count and Say
1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【BZOJ-1833】count数字计数 数位DP
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2494 Solved: 1101[Submit][ ...
- codeforces 710E E. Generate a String(dp)
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...
- 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
随机推荐
- press_keycode API 参数查询
用法 driver.press_ keycode(‘4’) 参数查找url:https://www.cnblogs.com/larack/p/4223465.html
- 垃圾收集器之:CMS收集器
HotSpot JVM的并发标记清理收集器(CMS收集器)的主要目标就是:低应用停顿时间.该目标对于大多数交互式应用很重要,比如web应用.在我们看一下有关JVM的参数之前,让我们简要回顾CMS收集器 ...
- vSphere client 登陆ESXI主机“您无权登录次服务器”
vCenter安装在虚拟机上,安装好后想调整下内存,直接把虚拟机关闭了电源,突然一想服务器都被我关了,还拿什么修改内存,完蛋! 突然想起,在使用vCenter之前,都是用vsphere client ...
- Centos 6.5 升级python到版本2.7.12
查看python版本: python --version 1.下载Python-2.7.12 wget https://www.python.org/ftp/python/2.7.12/Python- ...
- Linux故障-bash-4.1$
#模拟故障:-bash-4.1$ [root@nodchen ~]# su - cisco[cisco@nodchen ~]$ \rm -f .*rm: cannot remove `.': Is a ...
- matplot 代码实例
matplot 代码实例 #!/usr/bin/env python # coding=utf-8 import numpy as np import matplotlib.pyplot as plt ...
- 最全的CSS浏览器兼容问题http://www.68design.net/Web-Guide/HTMLCSS/37154-1.html
最全的CSS浏览器兼容问题 来源:68design.net 作者:邓飞飞 2008年09月23日 14:17 网友评论:7条 点击:71865 CSS 对浏览器的兼容性有时让人很头疼,或许当你了解当中 ...
- 【Unix网络编程】 chapter5 TCP客户,服务器程序实例
chapter5 5.1 概述 5.2 TCP回射服务器程序:main函数 int main(int argc, char **argv) { int listenfd,connfd; pid_t c ...
- 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165233
Exp1 PC平台逆向破解 实验内容 一.基础知识点 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP指令即"空指令",执行到NOP指令时,CPU什么也不做,机 ...
- js-传送file
这是选择文件的标签 <input type="file" class="add-image-input"> 这是js实现传输文件 var addIm ...