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. FTP2

    FTP: 环境:windows, python 3.5功能:1.用户加密认证,可自行配置家目录磁盘大小2.多用户登陆3.查看当前目录(家目录权限下)4.切换目录(家目录权限下)5.上传下载,进度条展示 ...

  2. python学习总结---函数使用 and 装饰器

    # 函数使用 ### 零碎知识 - 灵活的if-else ```python a = 3 if False else 5 print(a) ''' if False: a = 3 else: a = ...

  3. .NET Core Linux 部署实践

    部署环境:CentOS 7 x64 必要条件:当前用户有sudo权限 1. 安装依赖包sudo yum install libunwind libicu2. 下载.net core安装文件curl - ...

  4. HDU 4750 Count The Pairs (离线并查集)

    按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...

  5. ESLint 代码检查规范

    目录 Airbnb Javascript Style Guide 引用 对象 数组 函数 箭头函数 类和构造器 模块 Iterators and Generators 变量 比较运算符 注释 空格 A ...

  6. Java Web Action DAO Service层次理解

    参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...

  7. C#中不用安装Oracle客户端连接Oracle数据库(转)

    原文地址:http://www.cnblogs.com/jiangguang/archive/2013/02/19/2916882.html 0.首先,从Oracle网站上下载对应版本的Oracle ...

  8. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. 7月9日day1总结

    今天的学习过程和小结 上午学习了前端包括html,CSS,js等基本内容 前端10.25.134.187 html js css 1.块元素 ---默认占满整行,如果设置了高度和宽度都有效,如果不设置 ...

  10. Android应用开发EditText文本内容变化监听方法

    import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android. ...