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 ...
随机推荐
- 数据结构 -- 简单图的实现与遍历 (Java)
---恢复内容开始--- 作者版权所有,转载请注明出处,多谢. http://www.cnblogs.com/Henvealf/p/5534071.html 前些天因为某些原因,就试着回想一下图(gr ...
- openstack 实例迁移
- 老 base64 for xe8
not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...
- SpringMVC处理Date类型的成员变量方法
原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...
- LIS (最长上升子序列)
LIS两种写法 O(n^2) dp[i]表示以a[i]结尾的为LIS长度 #include <algorithm> #include <iostream> #include & ...
- How Tomcat Works(十七)
在前面的文章中,已经学会了如何通过实例化一个连接器和容器来获得一个servlet容器,并将连接器和容器相关联:但在前面的文章中只有一个连接器可用,该连接器服务8080端口上的HTTP请求,无法添加另一 ...
- ios和android一并学习的体会
如果说为什么要同时学习这两种不同的移动平台,其实有一定的“闲”的因素在里面. 相对于ios,android我是早半年接触的.最开始学习的时候也就是j2ee学习的延续,通过看视频连带看书学了大概一个月的 ...
- MySQL主从复制的原理及配置
[http://www.jb51.net/article/50053.htm] MySQL 数据库的高可用性架构: 集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单 ...
- Linux 中使用 KVM
from:http://www.php-oa.com/2010/02/22/ubutnu-kvm-vmware.html 听讲XEN很快就要从LINUX内核中去掉,redhat和Ubuntu也大力的在 ...
- jquery 显示“加载状态 结束”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...