[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), ...
随机推荐
- C#写Windows Service(windows服务程序)
背景: 要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...
- 【POI】使用POI处理xlsx的cell中的超链接 和 插入图片 和 设置打印区域
使用POI对xlsx中插入超链接和 插入图片 package com.it.poiTest; import java.awt.image.BufferedImage; import java.io.B ...
- C#开发微信公众平台-就这么简单(附Demo)(转)
原文:http://www.cnblogs.com/xishuai/p/3625859.html 写在前面 阅读目录: 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文. ...
- C语言补码作用
补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...
- jquery replace用法汇总
//只替换匹配到的第一个目标 var str="Visit Microsoft! Microsoft"document.write(str.replace(/Microsoft/, ...
- VS2013单元测试及代码覆盖率分析--Xunit
1,Javaweb中有jmeter.jacoco.ant.badboy等集成测试代码覆盖率的方式,C#代码的覆盖率怎么测试呢?VS2013的IDE上本身并未集成测试的工具,以下讲解VS2013中C#代 ...
- Android手机自动化测试真机运行
一, 打开手机的USB调试模式 不同的手机有不同的方法打开usb调试模式,可是去网上查一下你手机的调试模式打开办法(http://wenku.baidu.com/view/3077f06c25c ...
- linux命令缩写及全称
apt = Advanced Packaging Tool ar = archiver as = assembler awk = "Aho Weiberger and Kernighan&q ...
- ASCIL码和字符的转换
1.在python中: 字符-->ASCIL 用ord函数 ASCIL-->字符 用chr函数 下面是一个输入小写字母转换为大写字母输出的例子: # -*- coding: utf-8 - ...
- jquery(ajax)与js(ajax)的比较
原始js: function update_mess(){ var account_name = $("#account").val(); var xmlhttp; if(wind ...