leetcode: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.
Note: The sequence of integers will be represented as a string.
题目意思是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推(不断数数),写个countAndSay(n)函数返回字符串。
class Solution {
public:
string convert(const string &say)
{
stringstream ss; //创建一个流
int count=0;
char last=say[0];
for(size_t i=0; i<=say.size(); ++i)
{
if(last==say[i]) //每次与前一个字符比较是否相等,相等则计数加一
{
++count;
}
else
{
ss<<count<<last; //将count的值传递到流ss中,再将last的值传入
count=1;
last=say[i];
}
}
return ss.str(); //返回流中的字符串
}
string countAndSay(int n) {
if(n<=0) return string();
string say="1"; //定义初始字符串的内容
for (int i=1; i<n; ++i)
{
say=convert(say);
}
return say;
}
};
或:
class Solution {
public:
string nextRead(string s) {
stringstream ss;
int count, i = 0, n = s.length();
while (i < n) {
count = 0;
while (i + 1 < n && s[i] == s[i + 1]) {
i++;
count++;
}
ss << count + 1 << s[i];
i++;
}
return ss.str();
}
string countAndSay(int n) {
string res = "";
if (n == 0) return res;
res = "1";
if (n == 1) return "1";
while (n > 1) {
res = nextRead(res);
n--;
}
return res;
}
};
其他解法:
class Solution {
public:
string countAndSay(int n) {
if (n==0){
return NULL;
}
if (n==1){
return "1";
}
int count=1;
string s1="1";
string s2;
int j=1;
if (n>=2){
s1="11";
j=2;
}
int i=0;
while (j<n && n>1){
s2="";
for (i=0;i<s1.size();i++){
for (int m=i;m<s1.size()-1;m++){
if (s1[m]==s1[m+1]){
count++;
i++;
}
if (s1[m]!=s1[m+1]){
break;
}
}
s2+=to_string(count)+s1[i];
count=1;
}
s1=s2;
j++;
}
return s1;
}
};
class Solution {
public:
string countAndSay(int n) {
if(n==0) return " ";
if(n==1) return "1";
string str="1";
string tmp;
for(int i=1;i<n;++i)
{
tmp.clear();
int cnt=1;
for(int j=0;j<str.size();++j)
{
while(j+1<str.size())
{
if(str[j]==str[j+1])
{
++cnt;
++j;
}
else
break;
}
tmp.push_back(cnt+'0');
tmp.push_back(str[j]);
cnt=1;
}
str=tmp;
}
return str;
}
};
leetcode:Count and Say的更多相关文章
- leetcode:Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- LeetCode:数据库技术【175-178】
LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...
- LeetCode:螺旋矩阵||【59】
LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...
- LeetCode:旋转链表【61】
LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...
- LeetCode:乘法表中的第K小的数【668】
LeetCode:乘法表中的第K小的数[668] 题目描述 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要 ...
随机推荐
- lca 最近公共祖先
http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...
- URAL
URAL 2035 输入x,y,c, 找到任意一对a,b 使得a+b==c&& 0<=a<=x && 0<=b<=y 注意后两个条件,顺序搞错 ...
- YEBIS
在往项目里加yebis做各种后处理 就是看起来很高大上的各种味精 我又热!泪!盈!眶!了 压缩包解开 有各种文档 恩哼~ 大概看了下找到sample build.... 直接就success了.... ...
- Nginx的accept_mutex配置分析
让我们看看accept_mutex的意义:当一个新连接到达时,如果激活了accept_mutex,那么多个Worker将以串行方式来处理,其中有一个Worker会被唤醒,其他的Worker继续保持休眠 ...
- topcoder 642
A:直接拆开字符串写就好了. 今天的题目比较容易一些: B:题目大意: 求最少的翻转次数,每次翻转i是对应 y%i==0都会翻转. 球所有翻转为off状态的最小次数: 从最小idx开始,依次做就好了, ...
- Yarn的服务库和事件库使用方法
事件类型定义: package org.apache.hadoop.event; public enum JobEventType { JOB_KILL, JOB_INIT, JOB_START } ...
- C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.
Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...
- Java 序列化的高级认识
序列化 ID 问题 情境:两个客户端 A 和 B 试图通过网络传递对象数据,A 端将对象 C 序列化为二进制数据再传给 B,B 反序列化得到 C. 问题:C 对象的全类路径假设为 com.inout. ...
- linux下关于gz和bz2压缩格式的常用操作技巧
.gz和.bz2都是linux下压缩文件的格式,有点类似windows下的.zip和.rar文件..bz2和.gz的区别在于,前者比后者压缩率更高,后者比前者花费更少的时间. 也就是说同一个文件,压缩 ...
- lintcode: 跳跃游戏 II
跳跃游戏 II 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例 给出数组A = ...