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.

思路:模拟过程就行。这里学到的一个东西是stl里有一个int转string的函数叫to_string()。以前一直都是用stringstream来转。。。

 class Solution {
public:
string countAndSay(int n) {
if (n < ) return "";
string res = "";
for (int i = ; i <= n; i++)
{
string tem;
char cur = res[];
int count = ;
for (int j = , n = res.size(); j < n; j++)
{
if (cur == res[j]) count++;
else
{
tem.append(to_string(count) + cur);
cur = res[j];
count = ;
}
}
tem.append(to_string(count) + cur);
res = tem;
}
return res;
}
};

Amazon面试题里有一道array length encoding,和这个题差不多,只不过给的是一个int数组,返回的结果也是一个int数组,且只需要编码一次。

 class Solution
{
public:
vector<int> ArrayLengthEncoding(vector<int>& bits)
{
vector<int> res;
if (!bits.size()) return res;
int cur = bits[], count = ;
for (int i = , n = bits.size(); i < n; i++)
{
if (bits[i] == cur) count++;
else
{
res.push_back(cur);
res.push_back(count);
cur = bits[i];
count = ;
}
}
//we need to add the last part of bits
res.push_back(cur);
res.push_back(count);
return res;
}
};

Count and Say (Array Length Encoding) -- LeetCode的更多相关文章

  1. check the element in the array occurs more than half of the array length

    Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...

  2. 【转】The magic behind array length property

    Developer deals with arrays every day. Being a collection, an important property to query is the num ...

  3. [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.

    写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...

  4. poj-1782 Run Length Encoding

    http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS   Memory Limit: 30000K Total S ...

  5. 缓存 Array.length 是老生常谈的小优化

    问题 缓存 Array.length 是老生常谈的小优化. // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = ...

  6. Array.length vs Array.prototype.length

    I found that both the Array Object and Array.prototype have the length property. I am confused on us ...

  7. Math.floor(Math.random() * array.length),splice

    1.Math.floor(Math.random() * array.length) 返回长度内的索引 eg: changeLimit () { function getArrayItems(arr, ...

  8. javascript change array length methods

    javascript change array length methods Array 改变数组长度的方法 push, pop shift, unshift, splice, fill, 不改变数组 ...

  9. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

随机推荐

  1. chromedriver版本支持的Chrome版本

    下载chromedriver,链接:http://chromedriver.storage.googleapis.com/index.html   chromedirver版本 支持的Chrome版本 ...

  2. ffifdyop

    题目地址:http://www.shiyanbar.com/ctf/2036 后台登陆 上来看到这个界面,果断先看一波源代码. 看到是拼接字符串进行sql查询,就想到了注入了. 但是很不幸的是md5( ...

  3. linux服务器基本安全配置手册

    转:忘了在哪转的,直接复制到笔记里了,贴出来分享 假如你想要搭建一个Linux服务器,并且希望可以长期维护的话,就需要考虑安全性能与速度等众多因素.一份正确的linux基本安全配置手册就显得格外重要. ...

  4. Android记事本08

    昨天: Anr问题异常的原因和解决方案. 今天: Activity数据传递之通用方式. Activity数据传递之静态变量. Activity数据传递之全局变量. 遇到的问题: 无.

  5. 团队Alpha版本(九)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  6. 锚点自适应 map area coords

    最近做MOBILE的HTML5开发,人体图和页面一样,需要自适应不同的手机屏幕,蛋疼的是coords里面的标记是固定的,图片自适应后,锚点的标记就会产生空白区域,看了下https://github.c ...

  7. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  8. 情报传递(message)

    情报传递(message) 题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络,情报网络中共有n名情报员.每名情报员可能有若干名(可能没有)下线,除1名大头目外其余n-1名情报员有且仅有1名 ...

  9. 《R语言实战》读书笔记--第二章 创建数据集

    2.1数据集的概念 变量的类型是不同的,比如标示符.日期变量.连续变量.名义变量.有序型变量等,记得数据挖掘导论中有专门的描述. R可以处理的数据类型包括了数值型.字符型.逻辑型.复数型(虚数).原生 ...

  10. spring in action 学习笔记十三:SpEL语言(Spring Expression Language)

    SpEl语言的目的之一是防止注入外部属性的代码硬代码化.如@Value("#{student.name}")这个注解的意思是把Student类的name的属性值注入进去.其中stu ...