[Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.
Given an integer n, generate the n th sequence.
Note: The sequence of integers will be represented as a string.
题意:返回第n个序列,第i+1个字符串是第i个字符串的读法。参考:Grandyang和JustDoIT的博客。
思路:算法就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。代码需要两个循环,第一个是为找到第n个,第二是为了,根据上一字符串的信息来实现当前的字符串。
class Solution {
public:
string countAndSay(int n)
{
if(n<) return NULL; string res="";
for(int i=;i<n;++i)
{
string temp; //当前序列
res.push_back('*');
int count=; //重复的个数
for(int j=;j<res.size();++j)
{
if(j==)
count++;
else
{
if(res[j] !=res[j-])
{
temp.push_back(count+'');
temp.push_back(res[j-]);
count=;
}
else
++count;
}
}
res=temp;
}
return res;
}
};
[Leetcode] count and say 计数和说的更多相关文章
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
随机推荐
- ZooKeeper(1)-入门
一. Zookeeper工作机制 二.Zookeeper特点 三.Zookeeper数据结构 四.Zookeeper应用场景 统一命名服务 统一配置管理 统一集群管理 服务器动态上下线 软负载均衡
- 幸运三角形 南阳acm491(dfs)
幸运三角形 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 话说有这么一个图形,只有两种符号组成(‘+’或者‘-’),图形的最上层有n个符号,往下个数依次减一,形成倒 ...
- maven 添加自己下载的jar包到本地仓库
1.在pom文件中添加依赖,其中groupId等变量都自拟. 例如: 2.在命令行执行以下命令,提示build success即表示安装成功. mvn install:install-file -Dg ...
- Mysql综合练习作业50题
#作业库create database db8 charset utf8; #年级表create table class_grade(gid int not null primary key auto ...
- 从装机到配置-CentOS6.5
L006课程结束后的总结 首先:系统(cat /etc/redhat-release):CentOS release 6.5 (Final) 版本(uname -r):2.6.32-431.el6.x ...
- Apache 服务器性能评估
1 查看当前并发连接数 netstat -an | grep ESTABLISHED | wc -l 2 查看当前进程数 ps aux|grep httpd|wc -l
- define 和 const常量有什么区别?
define在预处理阶段进行替换,const常量在编译阶段使用 宏不做类型检查,仅仅进行替换,const常量有数据类型,会执行类型检查 define不能调试,const常量可以调试 define定义的 ...
- 【APUE】Chapter4 File and Directories
4.1 Introduction unix的文件.目录都被当成文件来看待(vi也可以编辑目录):我猜这样把一起内容都当成文件的原因是便于统一管理权限这类的内容 4.2 stat, fstat, fst ...
- 【JS笔记】闭包
首先看执行环境和作用域的概念.执行环境定义了变量或函数有权访问的其他数据,决定它们的行为,每个执行环境都有一个与其关联的变量对象,保存执行环境中定义的变量.当代码在一个环境中执行时,会创建变量对象的一 ...
- Python全栈 MongoDB 数据库(数据的修改)
修改操作符的使用 $set 修改一个域的值,增加一个域 阿哲年龄修改为33 db.class1.update({name:'阿哲'},{$set:{age:33}}) 如果sex域不存在则 ...