(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 ...
随机推荐
- VSPM虚拟串口使用
(1)打开虚拟串口工具,当你设置好你程序中的串口信息后,打开程序中的串口,然后虚拟串口中所显示的就是程序的所提供的串口信息 (2)选中其中一个串口,修改管理信息,点击”重新连接“ , 直接在管理那里, ...
- c++ insert iterators 插入型迭代器
insert iterators 插入型迭代器 (1)front inserters 前向插入迭代器 只适用于提供有push_front()成员函数的容器,在标准程序库中这样的容器是deque和lis ...
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- 51NOD 算法马拉松12
OTZ做出题目的神犇..断断续续改完了在这里存一下思路吧 A题:第K大区间题意:定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 分析:二分答案mid,任务 ...
- 命令行安装KVM
查看libvirtd的状态: [root@super67 ~]# /etc/init.d/libvirtd status libvirtd (pid 2503) is running... 安装vn ...
- [转] - 如何用QTcpSocket传送图片
我们知道,tcp网络编程发送数据是利用套接字来实现,将要传输的东西转化为数据流再进行传输,为了确保数据传输的准确性和安全性,我们在发送数据流前发送一个quint32的常量来表示所要发送的数据的大小:当 ...
- Html - 404页面
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Redis 笔记与总结2 String 类型和 Hash 类型
Linux 版本信息: cat /etc/issue 或cat /etc/redhat-release(Linux查看版本当前操作系统发行版信息) CentOS release 6.6 (Final) ...
- Failed to connect to remote VM. Connection refused. Connection refused: connect.
eclipse debug启动经常出现这个错误,已经启动了debug进程,X掉重新启动即可.
- NEC学习 ---- 布局 -两列, 右侧定宽,左侧自适应
该篇必须引用初始化样式和功能性样式,样式在前篇 http://www.cnblogs.com/Zell-Dinch/p/4436054.html 中已经提及. 上篇中介绍了左侧定宽,右侧自适应的布局, ...