leetcode报数题
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
1. 1 2. 11 3. 21 4. 1211 5. 111221
1
被读作 "one 1"
("一个一"
) , 即 11
。
11
被读作 "two 1s"
("两个一"
), 即 21
。
21
被读作 "one 2"
, "one 1"
("一个二"
, "一个一"
) , 即 1211
。
给定一个正整数 n ,输出报数序列的第 n 项。
注意:整数顺序将表示为一个字符串。
示例 1:
输入: 1 输出: "1"
示例 2:
输入: 4 输出: "1211"
//报数;
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:string countAndSay(int n) {if (n == 1)return "1";else {string output = countAndSay(n - 1);string result;int index = 0;int cur_step = index;while (index <output.size()){char curr_ch = output[index];int count = 0;while (output[cur_step] == curr_ch &&cur_step<=output.size()){cur_step++;count++;}char number = count + '0'; //!!!!将int型转变为char型;数字2的char型是50,所以加‘0’;result += number;result += curr_ch;index = cur_step;}return result;}}
};int main()
{Solution item;cout << item.countAndSay(6) << endl;system("pause");return 0;
}
leetcode报数题相关推荐
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) 15.3% 中 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录10——434. Number of Segments in a String(easy)
LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...
- LeetCode刷题记录9——58. Length of Last Word(easy)
LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...
- LeetCode刷题记录8——605. Can Place Flowers(easy)
LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...
最新文章
- mysqlls_mysql基本命令
- python request_python爬虫03 | 那个叫做 Urllib 的库让我们的 python 假装是浏览器
- adb查看手机cpu使用率_记录一下Unity打包Android在骁龙cpu上概率性卡死的问题
- 数字在数组中出现的次数
- ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQLEF
- where is ConstraintViolationException raised
- 如何删除github远程仓库?三部搞定!
- 你还在做描述性数据分析吗?卡方检验案例实操
- oracle关键字作为字段名使用方法
- 由Java编写的在线教育系统源码有何优势?
- 装载机液压系统原理图分析方法
- 生物统计分析之主成分分析(PCA)
- excel减法函数_在excel中进行减法7个例子,包括公式中带条件的减法
- Cadence Allegro PCB 17.4的叠层、正片和负片的设计
- 程序架构之redis扩容 (增加端口)
- Android蓝牙音乐
- 微信原生组件|基于小程序实现音视频通话
- 自动化提升系列:在白色背景上拍摄白色产品
- c语言中输出1st,高等学校计算机等级考试C语言模拟试题1st-all.doc
- 55.【Java 线程】
热门文章
- python实现QQ第三方登录
- php实现大文件分片上传
- IB书单推荐:没有所谓的“闲书”
- 【算法实践】他山之石, 可以攻玉 -- 利用完全二叉树快速实现堆排序
- jquery H5 好用的编辑器umeditor
- 服务中心-ServiceCenter
- rbenv安装Ruby2.3.5报错BUILD FAILED (Ubuntu 18.04 using ruby-build 20191225-1-gbac1f1c) openssl
- html页面添加大于号,2.7 在 HTML5 页面中插入半角的大于号“ ” , 使用的标记符应该是( )...
- java泛型的上界和下界_java泛型中的上界(extend)和下界(super)
- 24 个必须掌握的数据库⾯试题