class Solution {
public:
string calcuate(string s)
{
string result;
char pre = s[0];
int cnt = 1;
for(int i =1;i < s.length();i++)
{
if(s[i] == pre)
{
cnt++;
}
else
{
char tmp = cnt+'0';
result += tmp + pre;
pre = s[i];
cnt=1;
} }
char tmp = cnt + '0';
result += tmp + pre; return result;
}
string countAndSay(int n) {
string ret;
ret = "1";
int j = 1;
while(j < n)
{
ret = calcuate(ret);
j++;
}
return ret;
}
};

  上面方法错误 原因是由于使用 result += tmp +pre;这个操作

下面是修改后的代码:
class Solution {
public:
string calcuate(string s)
{
string result;
char pre = s[0];
int cnt = 1;
for(int i =1;i < s.length();i++)
{
if(s[i] == pre)
{
cnt++;
}
else
{
char tmp = cnt+'0';
result = result + tmp + pre;
pre = s[i];
cnt=1;
} }
char tmp = cnt + '0';
result = result+ tmp + pre; return result;
}
string countAndSay(int n) {
string ret;
ret = "1";
int j = 1;
while(j < n)
{
ret = calcuate(ret);
j++;
}
return ret;
}
};

  

(leetcode) countandsay的更多相关文章

  1. leetcode — count-and-say

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  2. [LeetCode] Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  3. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  4. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. Count and Say leetcode

    题目链接 The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 11 ...

  7. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  8. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  9. [LeetCode]题解(python):038-Count and Say

    题目来源 https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of inte ...

随机推荐

  1. BZOJ4399 : 魔法少女LJJ

    将所有权值离散化,建立权值线段树,维护区间内数字个数以及对数的和,用于比较乘积大小. 对于每个连通块维护一棵权值线段树,合并时用线段树合并. 对于操作3和4,暴力删除所有不合法节点,然后一并修改后插入 ...

  2. BZOJ3821 : 玄学

    对操作建立线段树,每个节点维护一个有序的操作表,表示用$[l,r]$的操作在每段区间上的作用效果. 对于一个线段树节点,合并左右儿子信息只在该区间刚刚被填满时进行,利用归并排序,时间复杂度为$O(n\ ...

  3. 洛谷 P1313 计算系数 Label:杨辉三角形 多项式计算

    题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别为 a ,b ,k , ...

  4. 【BZOJ】1854: [Scoi2010]游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题意:n个数据,每个数据有两个属性,要求取一些数据且每个数据取一个属性使得组成连续的一段单调递 ...

  5. oracle系列--第五篇 PLSQL连接本地的Oracle数据库

    这篇blog主要是针对新手,我也是个新手:) 我们把oracle成功的安装在了我们的计算机上面,那我们如何才能将PLSQL developer连 接到本地的oracle呢? 首先,我们必须有下面步准备 ...

  6. 字符串分割与存入List集合

    List<string> namelist = new List<string>(); string[] namejh = null; string name= "张 ...

  7. CSS中a标签样式的“爱恨”原则

    CSS为一些特殊效果准备了特定的工具,我们称之为“伪类”.其中有几项是我们经常用到的,下面我们就详细介绍一下经常用于定义链接样式的四个伪类,它们分别是: 1 :link 2 :visited 3 :h ...

  8. #undef

    #undef 是在后面取消以前定义的宏定义 该指令的形式为 #undef 标识符 其中,标识符是一个宏名称.如果标识符当前没有被定义成一个宏名称,那么就会忽略该指令. 一旦定义预处理器标识符,它将保持 ...

  9. 在openGL中绘制图形

    点的绘制.: glVertex*():星号表示函数要有后缀 该函数 需要放在glBegin函数和glEnd函数之间,glBegin函数的向量指定绘制图元的类型,而glEnd函数没有参数,例如: glB ...

  10. c++ windows 获取mac地址

    c++ windows 获取mac地址 GetAdaptersInfo 用windows api获取mac地址和硬盘id编号 aa