lintcode-420-报数
420-报数
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
1, 11, 21, 1211, 111221, ...
- 1 读作 "one 1" -> 11.
- 11 读作 "two 1s" -> 21.
- 21 读作 "one 2, then one 1" -> 1211.
给定一个整数 n, 返回 第 n 个顺序。
注意事项
整数的顺序将表示为一个字符串。
样例
给定 n = 5, 返回 "111221".
标签
字符串处理 脸书
思路
看懂题,找到报数规律,就可以写出程序
- n = 1,result = "1",这是初始数据
- n = 2,result = "11",意味着上一个报数的结果是 "1个1"
- n = 3,result = "21",意味着上一个报数的结果是 "2个1"
- n = 4,result = "1211",意味着上一个报数的结果是 "1个2、1个1"
- n = 5,result = "111221",意味着上一个报数的结果是 "1个1、1个2、2个1"
- ...
code
class Solution {
public:
/*
* @param n: the nth
* @return: the nth sequence
*/
string countAndSay(int n) {
// write your code here
if (n <= 0) {
return string("");
}
string result("1");
for (int i = 1; i < n; i++) {
string temp;
int count = 1, j;
for (j = 0; j < result.size() - 1; j++) {
if (result[j] == result[j + 1]) {
count++;
}
else{
temp += ('0' + count);
temp += result[j];
count = 1;
}
}
if (j > 0 && result[j] == result[j - 1]) {
temp += ('0' + count);
temp += result[j];
}
else if (j == 0) {
temp += '1';
temp += result[j];
}
else {
temp += '1';
temp += result[j];
}
result = temp;
}
return result;
}
};
lintcode-420-报数的更多相关文章
- lintcode :Count and Say 报数
题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 【LeetCode/LintCode 题解】约瑟夫问题 · Joseph Problem
n个人按顺序围成一圈(编号为1~n),从第1个人从1开始报数,报到k的人出列,相邻的下个人重新从1开始报数,报到k的人出列,重复这个过程,直到队伍中只有1个人为止,这就是约瑟夫问题.现在给定n和k,你 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
随机推荐
- error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
今天在使用easywechat对接企业打款到银行卡时,遇到了两个错误 error:0906D064:PEM routines:PEM_read_bio:bad base64 decode 和 erro ...
- PHP 查找二维数组中是否有指定字符串的字段
Array ( ] => Array ( [content] => 您提交了订单,请等待系统确认 :: [operator] => 客户 ) ] => Array ( [con ...
- appache 端口 更改
外网访问---->hosts文件映射服务名(127.0.0.1 xiaotian.cn)-->appache中httpd文件监听相关端口号(*:8080)--->appache中的v ...
- hive表查询中文显示乱码
hive在查询表信息时,中文显示乱码,数字或者url显现null问题解决思路. 1.确定create hive表时指定的row format delimited fields terminated b ...
- Python调用time模块设置当前时间-指定时间
import datetimeimport time#新建元旦时间#将程序打包def A(): # 设定时间 newyear =datetime.datetime(2033,10,1) #调用当前时间 ...
- Python用@property使类方法像属性一样访问
class Screen(object): @property #读取with的值getter方法 def width(self): return self._width @width.setter ...
- Go 学习之路:引用类型与值类型
Golang中只有三种引用类型:slice(切片).map(字典).channel(管道): 引用类型 引用类型理解为(C语言):指针 值类型 值的拷贝 下面以值类型和slice(切片)例子可知: p ...
- lncRNA芯片重注释
.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table ...
- IDL返回众数(数组中出现次数最多的值)
对于整型数组,可以直接利用histogram函数可以实现,示例如下: IDL>array = [1, 1, 2 , 4, 1, 3, 3, 2, 4, 5, 3, 2, 2, 1, 2, 6, ...
- 20155331 实验四 Android开发基础
20155331丹增旦达实验四报告 实验四 Android程序设计-1 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java ...