(leetcode) countandsay
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的更多相关文章
- leetcode — count-and-say
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [LeetCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- Count and Say leetcode
题目链接 The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 11 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [LeetCode]题解(python):038-Count and Say
题目来源 https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of inte ...
随机推荐
- 【BZOJ】1069: [SCOI2007]最大土地面积(凸包+旋转卡壳)
http://www.lydsy.com/JudgeOnline/problem.php?id=1069 显然这四个点在凸包上,然后枚举两个点找上下最大的三角形即可. 找三角形表示只想到三分QAQ.. ...
- 使用C++/C qsort 标准库对结构体进行快速排序
C++标准快速排序库qsort进行结构体快速排序 代码如下 #include <stdio.h> #include <stdlib.h> typedef struct { in ...
- Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
一.Servlet 生命周期 1.加载 2.实例化 3.初始化 4.处理请求 5.销毁 二.Servlet与cgi的区别: Servlet处于服务器进程中,它通过多线程方式运行其service方法,一 ...
- SSH全注解开发
web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&quo ...
- .NET生成静态页面的方案总结
转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...
- PreparedStatement与Statement的区别
PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...
- Powershell变量的类型
Powershell 默认支持的.NET类型如下: [order], [pscustomobject], [array], [bool], [byte], [char], [datetime], ...
- beta-2阶段组员贡献分分配
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 bera-2阶段各组员的贡献分分配如下: 姓名 个人工作量 组长评价 个人评价 团队贡献总分 胡丽娜 9 4 4 4.25 林莉 9 4 ...
- 提取安卓手机的recovery
一直都是从网上下载的recovery文件安装到手机.至于这个小小的recovery到底是什么全然不知.能不能自己做一个recovery呢?因为功能比较多的clockworkmod(简称cmw)的官网上 ...
- Web 在线文件管理器学习笔记与总结(10)查看文件夹中的内容
① 读取文件夹大小 a. 封装计算文件夹大小的函数 b. 打开文件夹 c. 循环判断文件夹下的内容是文件还是文件夹,如果是文件,则累积相加文件的大小:如果是文件夹,则递归调用该函数 注意两个问题: ...