[LintCode] 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.
Notice
The sequence of integers will be represented as a string.
Given n = 5, return "111221".
LeetCode上的原题,请参见我之前的博客Count and Say。
class Solution {
public:
/**
* @param n the nth
* @return the nth sequence
*/
string countAndSay(int n) {
if (n < ) return "";
string res = "";
while (--n) {
res.push_back('#');
string t = "";
int cnt = ;
for (int i = ; i < res.size(); ++i) {
if (res[i] == res[i - ]) ++cnt;
else {
t += to_string(cnt) + res[i - ];
cnt = ;
}
}
res = t;
}
return res;
}
};
[LintCode] Count and Say 计数和读法的更多相关文章
- [LeetCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- [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 and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- lintcode 466. 链表节点计数
466. 链表节点计数 计算链表中有多少个节点. 样例 给出 1->3->5, 返回 3. /** * Definition of ListNode * class ListNode ...
- [Leetcode] count and say 计数和说
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- D. Count the Arrays 计数题
D. Count the Arrays 也是一个计数题. 题目大意: 要求构造一个满足题意的数列. \(n\) 代表数列的长度 数列元素的范围 \([1,m]\) 数列必须有且仅有一对相同的数 存在一 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...
随机推荐
- Chrome书签被篡改之后的恢复
chrome书签和备份存放的路径:(XXXX为用户名)(AppData文件夹为隐藏文件夹) \Users\XXXX\AppData\Local\Google\Chrome\User Data\Defa ...
- Java学习笔记(十)——多态
一.多态 1.对象的多种形态 (1)引用多态: 父类的引用可以指向本类的对象 父类的引用可以指向子类的对象 (2)方法多态: 创建本类对象时,调用的方法为本类方法: 创建子类对象时,调用的方法是子类方 ...
- 【spring】 <tx:annotation-driven /> 的理解 【转载的】
在使用SpringMvc的时候,配置文件中我们经常看到 annotation-driven 这样的注解,其含义就是支持注解,一般根据前缀 tx.mvc 等也能很直白的理解出来分别的作用.<tx: ...
- Android自动化测试之Monkey Test(一)
Monkey是什么 Monkey是可以运行在模拟器里或实际设备中的程序.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试. Monkey简 ...
- Maven 项目导入错误解决。
Description Resource Path Location Type Failure to transfer org.apache.maven:maven-core:jar:2.0.6 fr ...
- position之absolute与relative 详解
absolute:绝对定位: relative:相对定位: 唉,以前只是知是知道这两个单词的汉语意思,然后呢,,,怎么用...也是摸凌两可的用.终于抽出时间来看看了: 1.绝对定位:absulute ...
- jQuery事件和JavaScript事件
1.JavaScript事件: 属性 当以下情况发生时,出现此事件 FF N IE onabort 图像加载被中断 1 3 4 onblur 元素失去焦点 1 2 3 onchange 用户改变域的内 ...
- 2016.9.14 JavaScript入门之七面向对象和函数
1.JavaScript中的变量,可以是对象,具有相应的属性: 2.我们也能够使用构造函数创建对象.构造函数的函数给出了一个大写的名称,以使它清楚地表明它是一个构造函数. 在构造函数中,这个变量是指由 ...
- 6754 Keyboard of a Mobile Telephone
/*实践再次说明ch=getchar()的速度非常慢*/ /*大水题,不解释*/ #include<stdio.h> #include<string.h> int main() ...
- 1、Delphi 打开目录和txt文件模块
//1.打开目录和打开txt文件 procedure TMainForm.bbtnOpenLoClick(Sender: TObject); var sLogName: string; begin s ...