Count and Say (Array Length Encoding) -- LeetCode
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的更多相关文章
- 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 ...
- 【转】The magic behind array length property
Developer deals with arrays every day. Being a collection, an important property to query is the num ...
- [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...
- poj-1782 Run Length Encoding
http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS Memory Limit: 30000K Total S ...
- 缓存 Array.length 是老生常谈的小优化
问题 缓存 Array.length 是老生常谈的小优化. // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = ...
- 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 ...
- Math.floor(Math.random() * array.length),splice
1.Math.floor(Math.random() * array.length) 返回长度内的索引 eg: changeLimit () { function getArrayItems(arr, ...
- javascript change array length methods
javascript change array length methods Array 改变数组长度的方法 push, pop shift, unshift, splice, fill, 不改变数组 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
随机推荐
- SpringMVC 整合 kaptcha(验证码功能)
一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...
- Spider_Man_6 の Scrapy_Downloader Middleware(针对一下🐷🐷🐷)
下载器中间件(Downloader Middleware) 下载器中间件是介于Scrapy的request/response处理的钩子框架.是用于全局修改Scrapy request和response ...
- Oracle 学习---- 练习语法 循环( loop end loop; for ;while; if elsif end if )
/*--set serveroutput on;declare mynum number(3) :=0; tip varchar2(10):='结果是 ';begin mynum:=10+100; d ...
- 2 28TOP100
import json import requests from requests.exceptions import RequestException import re import time d ...
- ZooKeeper客户端 zkCli.sh 节点的配额设置
首先使用 zkCli.sh 连接上ZooKeeper服务器 配额设置命令格式如下: setquota -n|-b val path -n:val设置子节点个数 -b:val设置节点的数据长度 如果我们 ...
- 【bzoj3675】[Apio2014]序列分割 斜率优化dp
原文地址:http://www.cnblogs.com/GXZlegend/p/6835179.html 题目描述 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列 ...
- P1194 买礼物
题目描述 又到了一年一度的明明生日了,明明想要买B样东西,巧的是,这B样东西价格都是A元. 但是,商店老板说最近有促销活动,也就是: 如果你买了第I样东西,再买第J样,那么就可以只花K[I,J]元,更 ...
- GeoIP2 数据库更新地址
GeoIP2 数据库更新地址 数据库文件下载网页地址 http://dev.maxmind.com/geoip/geoip2/geolite2/ http://geolite.maxmind.com/ ...
- spark与storm比对与选型
大数据实时处理平台市场上产品众多,本文着重讨论spark与storm的比对,最后结合适用场景进行选型. 一.spark与storm的比较 比较点 Storm Spark Streaming 实时计算模 ...
- [bzoj] 1036 Count
原题 树链剖分板子题 树剖详解: #include<cstdio> #include<algorithm> typedef long long ll; #define N 30 ...