(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 ...
随机推荐
- lal
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...
- shell条件与循环
一.if语句 if [expression] then elif[expression] then else fi 注 : expression前后要有空格:判断相等用 = 而不是 == : then ...
- ASCII Table - ASCII码对照表
ASCII控制字符 二进制 十进制 十六进制 缩写 可以显示的表示法 名称/意义 0000 0000 0 00 NUL ␀ 空字符(Null) 0000 0001 1 01 SOH ␁ 标题开始 00 ...
- 数组机、局域网ip查找
cmd ipconfig 以太网适配器 VMware Network Adapter VMnet8: IPv4 地址 . . . . . . . . . . . . : 192.168.233.1
- Java学习记录-注解
注解 一.org.springframework.web.bind.annotation ControllerAdviceCookieValue : 可以把Request header中关于cooki ...
- ps命令交叉编译
busybox中的ps命令是针对于嵌入式的,其中一些选项并不完整.因此需要将源码下载下来,进行交叉编译 官方下载地址 github下载地址 含有configure,我在此使用的是这个源码包,官方的包在 ...
- 嵌入式 如何定位死循环或高CPU使用率(linux) 及性能优化
嵌入式 如何定位死循环或高CPU使用率(linux) ln -s /mnt/nfs/_install/usr/bin/sort /usr/bin/sort awk '{print $1,$2,$14, ...
- 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈
刚入手了一本<程序员代码面试指南>,书中题目的代码都是 Java 实现的,琢磨着把这些代码用 PHP 敲一遍,加深印象. 题目:设计一个有 getMin 功能的栈 —— 实现一个特殊的栈, ...
- 优秀而又实用的PHP工具集锦
优秀而又实用的PHP工具集锦 浏览:1141 发布日期:2013/09/04 分类:技术分享 PHP是目前实用最为广泛的服务器端开源脚本语言之一,很多优秀的开源程序都是基于PHP构建的,比如大名鼎 ...
- Calculate its MTBF assuming 2000 FITS for each DRAM
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A common unit of meas ...