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)的更多相关文章

  1. LeetCode题解38.Count and Say

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

  2. 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 ...

  3. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  4. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

  5. 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 ...

  6. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  7. 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 ...

  8. 38. Count and Say

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

  9. 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 ...

随机推荐

  1. Linux下查看某个进程占用的CPU、内存

    1.用top命令指定固定的PID top -p 10997 查询指定进程的PID ps -ef | grep zookeeper jim 10997 1959 0 12月14 pts/2 00:00: ...

  2. VS2005常用快捷键

    Visual C++ 2005有很多种快捷键的映射方案,有适合 Emacs 用户的,有适合 Visual C++ 6.0 用户的,也有 Visual Studio 2005的,下面的快捷键符合IDE默 ...

  3. jquery拖动分页

    scrollpagination.js /* ** Anderson Ferminiano ** contato@andersonferminiano.com -- feel free to cont ...

  4. java.lang.ClassNotFoundException: org.I0Itec.zkclient.exception.ZkNoNodeException 异常 如何处理

    严重: Context initialization failed java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNo ...

  5. 代码生成器 CodeSmith 的使用(二)

    在第一篇中,简单的介绍了 CodeSmith 的使用方法,这次做一个生成简单的数据库字段属性的模板.以下只粘贴主要的代码片段. <%-- Name: Copyright © Sun 2013-2 ...

  6. javascript 常用获取页面宽高信息 API

    在页面的构建中 常常会需要获取页面的一些宽高信息,例如实现 惰性加载图片 需要获取页面的可见区域高度 和 已滚动区域的高度,以判断图片所在位置是否可见来决定加载图片的时间, 花点时间整理了一下,获取页 ...

  7. png-CRC32校验

    官方文档: https://www.w3.org/TR/PNG-CRCAppendix.html CRC32校验的数据,看原文 A four-byte CRC (Cyclic Redundancy C ...

  8. Spring 配置 web.xml (防止spring 内存溢出)

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  9. 4. mybatis实战教程(mybatis in action)之四:实现关联数据的查询

    转自:https://www.cnblogs.com/shanheyongmu/p/5653599.html 有了前面几章的基础,对一些简单的应用是可以处理的,但在实际项目中,经常是关联表的查询,比如 ...

  10. 浮动float 摆放位置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...