[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.
#include <string>
#include <iostream>
#include <sstream>
using namespace std; class Solution {
public:
string countAndSay(int n) {
if(n<=) return "";
if(n==) return "";
string curStr="",retStr="";
for(int i=;i<n;i++){
int cnt = ;
retStr="";
for(int j=;j<curStr.size();j++){
if(curStr[j-]==curStr[j]) cnt++;
else{
stringstream ss;
ss<<cnt<<curStr[j-];
retStr+=ss.str();
cnt=;
}
}
stringstream ss;
ss<<cnt<<curStr[curStr.size()-];
retStr+=ss.str();
curStr=retStr;
}
return retStr;
}
}; int main()
{
Solution sol;
for(int i=;i<;i++){
string ret=sol.countAndSay(i);
cout<<"i:"<<ret<<endl;
}
return ;
}
[LeetCode] Count and Say 字符串的更多相关文章
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- leetcode题解之分解字符串域名
1.题目描述 A website domain like "discuss.leetcode.com" consists of various subdomains. At the ...
- LeetCode.893-特殊相等字符串组(Groups of Special-Equivalent Strings)
这是悦乐书的第344次更新,第368篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第209题(顺位题号是893). You are given an array A of ...
- LeetCode初级算法之字符串:387 字符串中的第一个唯一字符
字符串中的第一个唯一字符 题目地址:https://leetcode-cn.com/problems/first-unique-character-in-a-string/ 给定一个字符串,找到它的第 ...
- 【LeetCode】839. 相似字符串组 Similar String Groups (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎 ...
- [LeetCode]1221. 分割平衡字符串
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
随机推荐
- mysql六:数据备份、pymysql模块
一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 掌握: #1. 测试+链接数据库 #2. 新建库 #3. 新建表,新增字段+类型+约束 #4. 设计表 ...
- node操作mogondb数据库的封装
注:摘自网络 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mo ...
- Ecshop之ajax修改表里的状态(函数化处理)
目录 功能: 效果: 思路: 页面里 控制器里 功能: `点击图片,修改表里的状态值` 效果: 思路: 页面里在img里点绑定onclick件事,调用js函数listTable.toggle oncl ...
- 关于Linux系统下zookeeper集群的搭建
1.集群概述 1.1什么是集群 1.1.1集群概念 集群是一种计算机系统, 它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系 ...
- 单片机入门学习笔记6:新唐单片机N76E003
学习新唐单片机是从2018年3月开始的,之前一点也不懂这一块单片机,之后脉络变的越来越清晰. 由于N76E003档次太低,新塘科技官方的管脚配置,芯片选型……都没有这一块芯片,资料唯独只有:芯片的数据 ...
- P1616 疯狂的采药【模板】
此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师. ...
- 《鸟哥的Linux私房菜》学习笔记(7)——grep及正则表达式
一.grep命令 grep:根据模式搜索文本,并将符合模式的文本行显示出来 模式:由文本字符和正则表达式的元字符组合成的匹配条件 命令格式:grep [options] [file...] [root ...
- easyui的layout
1.浏览器自适应(即浏览器改变大小,里面的表格大小也会随之改变)要设置两个参数 (1)一般都要在body上设置class=“easyui-layout”: <body class="e ...
- 【Restore IP Addresses 】cpp
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- IOS开发学习笔记008-预处理
预处理 1.宏定义 2.条件编译 3.文件包含 注意: 1.所有预处理都是以#开头,并且结尾不用分号. 2.宏名一般用大写字母,以便与变量名区别开来,但用小写也没有语法错误 3.作用域也是从定义到代码 ...