https://oj.leetcode.com/problems/count-and-say/

求经过n次变换后,变成了什么。

1  11  21  1211   111221

ps. 3 变成 ‘3’,为 3 + '0'

  1. class Solution {
  2. public:
  3. string countAndSay(int n) {
  4. string ans_str;
  5. vector<int> input;
  6. input.push_back(); // the first time
  7. vector<int> output;
  8. for(int i = ;i<n;i++)
  9. {
  10. fun(input,output);
  11. input = output;
  12. }
  13.  
  14. for(int i = ;i<input.size();i++)
  15. ans_str.push_back(''+input[i]);
  16. return ans_str;
  17. }
  18. void fun(vector<int> input,vector<int> &output)
  19. {
  20. output.clear();
  21. int itr = ;
  22. while(itr<input.size())
  23. {
  24. int value = input[itr];
  25. int times = ;
  26. while(itr<input.size()&&input[itr]==value)
  27. {
  28. times++;
  29. itr++;
  30. }
  31. output.push_back(times);
  32. output.push_back(value);
  33. }
  34. }
  35. };

LeetCode OJ-- Count and Say的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

随机推荐

  1. Tufurama CodeForces - 961E

    Tufurama CodeForces - 961E 题意:有一部电视剧有n季,每一季有ai集.问有多少对i,j存在第i季第j集也同时存在第j季第i集. 思路:核心问题还是统计对于第i季,你要统计第i ...

  2. mysql-update时where条件无索引锁全表

          1 5.3日数据处理需求 UPDATE md_meter set warranty_end_date = DATE_ADD(warranty_begin_date,INTERVAL 10 ...

  3. Collection record

    复习大集合: 1.函数的参数:位置参数,关键字参数,动态参数 2.命名空间:内置命名空间,全局命名空间,局部命名空间 3.闭包函数:函数引用未定义的函数外非全局的变量叫做闭包,该函数称为闭包函数 4. ...

  4. 【N-Quens II】cpp

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  5. Linux之如何进行固定IP、DNS等设置

    前提:虚拟机Linux centOS6.6 Linux如何设置固定IP.dns.网关 1.切换到root账号 2.#cd  /etc/sysconfig/network-scripts 进入网卡的设置 ...

  6. vmware克隆centos修改linux mac地址

    故障背景:  在vmware workstation中了完全克隆了一个已经存在的centos的虚拟机,启动之后发现网卡没有启动.于是重启一下network服务,发现提示错误信息“Device eth0 ...

  7. Wordpress 自定义文章类型添加 Categoried、Tags

    默认情况下 ,自定义文章类型没有分类和标签属性,需要通过 register_taxonomy_for_object_type 手动注册文章分类和标签,可以通过在 functions.php 或插件中添 ...

  8. redis linux 安装

    安装 1): wget http://download.redis.io/releases/redis-5.0.2.tar.gz 2): tar xzf redis-5.0.2.tar.gz 3):c ...

  9. C#知识点<4>

    1\C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的. ...

  10. AngularJs 特性 之 双向数据绑定

    <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="UT ...