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 ...
随机推荐
- CodeForces 622 A.Infinite Sequence
A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- ajax 新闻栏目
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 转】腾讯云CentOS 6.6安装 Nginx
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/5290155.html 感谢! 一.下载Nginx 从Nginx的官网(http://nginx.org/en/d ...
- jquery的clone方法bug的修复
最近发现jquery的clone的bug,textarea和select的jquery的clone方法有问题,textarea和select的值clone的时候会丢掉,在网上发现一个插件,下载地址如下 ...
- http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Data_Types
http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Data_Types
- 对Spring.Net的AOP一些思考及应用
前言 这几天在配置Spring.NET,配到AOP的时候发现自己现在还是没有理解到Spring AOP的实现,只是认识到了一个思想,以前配的时候,看的是给好的例子用,真正用的时候还是要想一下 ...
- hdoj 5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 这道题用到了Manacher算法,首先简单介绍一下Manacher算法: ----------- ...
- EasyMock 使用方法与原理剖析
from:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...
- jqGrid初次使用遇到的问题及解决方法
问题一:初始化定义翻页用的导航栏时,表中出现"undefined"方框: 解决:需要导入grid.locale-cn.js文件. 问题二:页面只有一页,无法翻页: 解决:初始化设置 ...
- .NET开发中的事务处理大比拼
本文转载:http://www.cnblogs.com/jhxk/articles/2696307.html http://liubaolongg.blog.163.com/blog/static/2 ...