394. Decode String
【题目】
- Total Accepted: 10087
- Total Submissions: 25510
- Difficulty: Medium
- Contributors: Admin
Given an encoded string, return it's decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
【解题】
class Solution {
public:
string decodeString(string s) {
stack<string> strs;
stack<int> counts;
string result = "";
int cnt = ;
for (int i = ; i < s.size(); i++) {
if(s[i] <= '' && s[i] >= '') {
cnt = cnt* + s[i] - '';
cout << "number = " << cnt << endl;
} else if (s[i] == '[') {
cout << "[" << endl;
counts.push(cnt);
strs.push(result);
result.clear();
cnt = ;
} else if (s[i] == ']') {
int cur_cnt = counts.top();
cout << "]" << " cur_cnt = " << cur_cnt << endl;
counts.pop();
for (int j = ; j < cur_cnt; j++) {
strs.top() += result;
}
result = strs.top();
strs.pop();
} else {
result += s[i];
cout << "result += " << s[i] << endl;
}
}
return strs.empty()? result:strs.top();
}
};
注意:
'['后记得要清空result,初始化cnt为0。
394. Decode String的更多相关文章
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394. Decode String 解码icc字符串3[i2[c]]
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- LC 394. Decode String
问题描述 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], wh ...
- 【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- Python 解LeetCode:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
随机推荐
- IOS 断点下载
// // ViewController.m // UI4_断点下载 // // Created by qianfeng on 15/8/19. // Copyright (c) 2015年 ZBC. ...
- GOLANG SDK下载
如果没有FQ的话是不能访问国外网站的,但是golang提供了中国站点,要下载sdk可以在中国站点下载 中国站点: http://www.golangtc.com/download
- DbContext 那些事 —— 数据库初始化
数据库初始化 上图,这个图解释了,数据库初始化的流程,是基于我们在上下文类中的构造函数中传递的参数. 在上面的图中,context类中的base构造器中,可以填入下面的参数: 无参数(No Param ...
- 简单播放器(增加sdl事件控制)
#include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscal ...
- ucos中的三种临界区管理机制
熟悉ucos,或者读过Jean.J.Labrosse写过的ucos书籍的人,一定会知道ucos中著名的临界区管理宏:OS_ENTER_CRITICAL()和OS_EXIT_CRITICAL(). 同样 ...
- 组合suan
/// 求从数组a[1..n]中任选m个元素的所有组合. /// a[1..n]表示候选集,n为候选集大小,n>=m>0. /// b[1..M]用来存储当前组合中的元素(这里存储的是元素 ...
- Linux下不同服务器间数据传输--转载
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...
- 关于Tomcat在eclipse上的配置
一:安装JDK(建议版本比较新的jdk,因为有很多集成于jdk软件需要的jdk版本比较高): jdk官网下载位置:http://www.oracle.com/technetwork/java/java ...
- medoo–高效的轻量级PHP数据库操作类
网址:http://medoo.lvtao.net/ /*medoo.php*/ class medoo { protected $database_type = ‘mysql’; protected ...
- 《开源安全运维平台:OSSIM最佳实践》内容简介
<开源安全运维平台:OSSIM最佳实践 > 李晨光 著 清华大学出版社出版 内 容 简 介在传统的异构网络环境中,运维人员往往利用各种复杂的监管工具来管理网络,由于缺乏一种集成安全运维平台 ...