【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 ...
随机推荐
- ABP模块配置
介绍 我们知道ABP中模块的配置都是通过模块的Configuration属性来设置的.例如在模块的生命周期方法中可以进行一系列的配置 审计 MQ Redis....也可以替换一些ABP默认配置 通常我 ...
- sqlserver 使用维护计划备份
https://www.cnblogs.com/teafree/p/4240040.html
- Java Swing 如何让窗体居中显示
如题,其他不多说,直接上代码! package com.himarking.tool; import java.awt.Toolkit; import javax.swing.JFrame; @Sup ...
- JS中,日期对象(获取当前现在的年份,星期,时间)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- re 模块 常用正则表达式符号 最常用的匹配语法
常用正则表达式符号1 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags MULTILINE, ...
- IE6中浮动双边距bug
想要创建出漂亮的网页设计, 除了要认真学习每一个html和CSS代码之外,不可能不去了解一下臭名昭著的IE6和更早的那些IE浏览器的坏脾气,因为你本来写出的规规矩矩的代码, 漂亮的设计就此就要完成了, ...
- Freemarker 使用注意事项
上次借助 Freemaker 模板引擎来动态构造 Kylin 访问的 SQL,在使用过程中,遇到了一些坑. ${} 输出变量时需要注意: 示例 WHERE shop_id = ${val} 里的 v ...
- Win 7系统优化/设置小工具 (脚本)
Win7系统优化脚本 用了多年win7,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win7的系统设置,比如更新提醒,关闭防火墙提示,烦人的系 ...
- 下拉刷新和上拉加载更多(第三方框架MJRefresh)
#import "RootViewController.h" #import "MJRefresh.h" @interface RootViewControll ...
- JAVA隐藏鼠标的方法
JAVA隐藏鼠标的方法 2012年06月03日 19:13:21 阅读数:2436 JAVA隐藏鼠标光标,可以有2种方法. 都是通过如下语句重新设置光标 Toolkit.getDefaultToolk ...