LeetCode38 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.(Easy)
分析:
按照题目要求以此生成新串即可,注意第一个是串是1,即下标从1开始。
同时,复习使用to_string.
代码:
class Solution {
public:
string countAndSay(int n) {
string s = "";
for (int i = ; i < n; ++i) {
int count = ;
string temp;
for (int j = ; j < s.size(); ++j) {
if (j == s.size() - || s[j] != s[j + ]) {
temp += to_string(count);
temp += s[j];
count = ;
}
else {
count ++;
}
}
s = temp;
}
return s;
}
};
LeetCode38 Count and Say的更多相关文章
- Leetcode字符串专题
Leetcode38. Count and Say 分析:根据题意,数列的下一项就是统计上一项中每个数字出现的次数,理解清楚题意就很简单了 class Solution { public: strin ...
- nodejs api 中文文档
文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...
- [Swift]LeetCode38. 报数 | Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- C#中Length和Count的区别(个人观点)
这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...
- [PHP源码阅读]count函数
在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...
- EntityFramework.Extended 实现 update count+=1
在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...
- 学习笔记 MYSQL报错注入(count()、rand()、group by)
首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...
- count(*) 与count (字段名)的区别
count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
随机推荐
- UltraEdit中文乱码的解决方法
现象问题: 同样的一个文件 UltraEdit 打开是乱码,显示文件的编码是U8-DOS,可是用EditPlus .记事本,打开,就是正常的,编码显示是ANSI. 即使在UltraEdit打开文件的时 ...
- 第三百二十五天 how can I 坚持
任何事情都是相对的,以后禁止专牛角尖. 今天在家堕落了一天,说好的把天气应用,照葫芦画瓢弄好,结果什么也没弄. 和你 有个毛线关系啊,哈哈,太逗了. 准备睡觉,一切随缘,反正想也没什么用,自己也搞不懂 ...
- Hadoop应用开发实战案例 第2周
比如,封面,是一网页,可以看出用户在此网页上,鼠标呈现F形状. 海量Web日志分析 用Hadoop提取KPI统计指标 更详细原文博客:http://blog.fens.me/hadoop-mapred ...
- spring的annotation-driven配置事务管理器详解
http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html ——————————————————————————————————————————————— ...
- 2013年度Python Git工具
Pycoders周刊根据读者对周刊文章的点击数据,评选出了2013年最受关注的和Git相关的Python工具. git-workflow (github.com) 可视化你的 git 工作流程的工具, ...
- linux 分区与挂载
流程:分区-格式化-挂载 1.添加硬盘 使用fdisk -l命令查看磁盘状态 此处可以看到两块硬盘sda和sdb,第一块硬盘sda是装好系统的.sdb硬盘是未进行分区的. 本例将这个20G的硬盘分区, ...
- [转]前景检测算法--ViBe算法
原文:http://blog.csdn.net/zouxy09/article/details/9622285 转自:http://blog.csdn.net/app_12062011/article ...
- POJ2155Matrix(二维线段树)
链接http://poj.org/problem?id=2155 题目操作就是说,每次操作可以是编辑某个矩形区域,这个区域的0改为1,1改为0,每次查询只查询某一个点的值是0还是1. 方法:二维线段树 ...
- 分布式文件系统FastDFS设计原理
原文地址: http://blog.chinaunix.net/uid-20196318-id-4058561.html FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker ...
- 译 - EF 6秘诀(第二版) - 目录
本博文系Entity Framework 6 Recipes, 2nd Edition的目录译文.保留原文,方便参考. 第一章 EF入门Chapter 1. Getting Started with ...