【Leetcode】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.
-----------------------------------------------------------------------------------------------
以上含义描述不清,题目的实际意思是:
n = 0: 1
n = 1: 11 (前一个是1个1)
n = 2: 21 (前一个是2个1)
n = 3: 1211 (前一个是1个2,1个1)
n = 4: 111221 (前一个是1个1,1个2,2个1)
....
所以是一个递推关系,按规则模拟即可。
 class Solution {
 public:
     string countAndSay(int n) {
         string result("");
         for (int i = ; i < n; ++i) {
             stringstream ss;
             int j = , N = result.size();
             while (j < N) {
                 int c = ;
                 while (j +  < N && result[j] == result[j + ]) {
                     ++c;
                     ++j;
                 }
                 ss << c << result[j];
                 ++j;
             }
             ss >> result;
         }
         return result;
     }
 };
【Leetcode】Count and Say的更多相关文章
- 【LeetCode】Count and Say(报数)
		这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ... 
- 【leetcode】Count and Say (easy)
		The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 
- 【leetcode】Count Primes(easy)
		Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ... 
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
		[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ... 
- 【LeetCode】Minimum Depth of Binary Tree   二叉树的最小深度 java
		[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ... 
- 27. Remove Element【leetcode】
		27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ... 
- 【leetcode】698. Partition to K Equal Sum Subsets
		题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ... 
- 【LeetCode】动态规划(下篇共39题)
		[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ... 
- 【LeetCode】二叉查找树 binary search tree(共14题)
		链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ... 
随机推荐
- jdbcTemplate学习(二)
			前面讲了增加.删除.更新操作,这节讲一下查询. 查询操作: (一)查询一个值(不需要注入参数) queryForObject(String sql, Class<T> requiredTy ... 
- Django的Model使用
			创建模型 使用Django的模型主要注意两个方面:字段的类型和方法的重写.这里用一个例子来说明,其中包含了常用的字段类型和如何重写方法. from django.db import models cl ... 
- str_place()替换函数
			str_replace() 函数使用一个字符串替换字符串中的另一些字符. 注释:该函数对大小写敏感.请使用 str_ireplace() 执行对大小写不敏感的搜索. echo str_replace( ... 
- Node.js的__dirname,__filename,process.cwd(),./的含义
			简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ... 
- Java3D读取3DMax模型并实现鼠标拖拽、旋转、滚轮缩放等功能
			/**-------------------------------------------------代码区--------------------------------------------- ... 
- JavaWeb面试题  有用
			ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获取数据,然后用JavaScript来操作DOM从而更新页面的局部显示. Ajax的优点: 1.最大的一点是页面 ... 
- ROS Learning-023  (提高篇-001) 准备工作 --- 安装一些必要的软件包
			ROS 提高篇-001 - 准备工作 - 安装一些必要的软件 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ... 
- c语言学习笔记 const变量
			在c语言的编程过程中经常会遇到有常数参加运算的运算,比如这种. int a=100*b; 这个100我们叫常数或者叫常量,但是程序中我们不推荐这种直接写常数的方法,有两个缺点. 第一是程序可读性差. ... 
- CF1030F Putting Boxes Together
			昨晚的比赛题.(像我这种蒟蒻只能打打div2) 题意 给你$n$个物品,每一个物品$i$,有一个权值$w_i$和一个位置$a_i$,定义移动一个物品$i$到位置$t$的代价为$w_i * \left ... 
- java全栈day12----final static 匿名对象 内部类 包的声明与访问
			final关键字概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承, 或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后, ... 
