【题目】

  • 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的更多相关文章

  1. [LeetCode] 394. Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  2. Leetcode -- 394. Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  3. 394. Decode String 解码icc字符串3[i2[c]]

    [抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...

  4. 394 Decode String 字符串解码

    给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...

  5. LC 394. Decode String

    问题描述 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], wh ...

  6. 【LeetCode】394. Decode String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  7. Python 解LeetCode:394 Decode String

    题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...

  8. 【leetcode】394. Decode String

    题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...

  9. LeetCode 394. 字符串解码(Decode String) 44

    394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...

随机推荐

  1. JAVA程序操作hbase的Maven配置pom.xml文件

    由于工作需要,用到Java连接Hbase读写数据.原来的方式是直接将依赖的jar包全部下载下来,然后工程里面引用. 现想重构下,改为使用Maven管理,遇到了各种坑,各种不好使,特总结下. <d ...

  2. kegg-kass注释--转载

    在注释KEGG的时候,一直用到kaas,具体kaas是个什么东东,简单的总结一下吧.     KEGG是由日本人搞的一个代谢图,收录基因和基因组的数据库,数据库可以分为 3大部分,基因数据库, 化学分 ...

  3. 执行Hadoop job提示SequenceFile doesn't work with GzipCodec without native-hadoop code的解决过程记录

    参照Hadoop.The.Definitive.Guide.4th的例子,执行SortDataPreprocessor作业时失败,输出的错误信息 SequenceFile doesn't work w ...

  4. an interview question(1)

    声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...

  5. iOS静态库及Framework 创建

    本文转自cocoachina,尊重作者的汗水. 讲述的非常透彻,有需要的朋友可以阅读实践.转载请注明出处 //=================以下留着备份==================// 在 ...

  6. [地图SkyLine二次开发]框架(1)

    项目介绍: 项目是三维地理信息系统的开发,框架MVC4.0 + EF5.0 + Extjs4.2 + SkyLine + Arcgis,是对SkyLine的二次开发. 项目快结束了,先给大家看一眼效果 ...

  7. web开发字符乱码问题

    java动态网页后台乱码问题总结 乱码可能出现的几块地方: 首先是浏览器和html之间采用的编码不一致 解决办法: 修改浏览器的编码格式 修改html页面的编码格式: <meta http-eq ...

  8. 【Java】变量类接口_学习笔记

    变量.类和接口 1.变量的类型 实例变量(不以static修饰) 成员变量 类变量(以static修饰) 所有变量 形参(方法签名中定义的变量) 局部变量         方法局部变量(在方法内定义) ...

  9. MVC使用Membership配置

    MVC的权限管理,环境是MVC4.5,SQL Server2008 修改前 Web.config文件: <system.web> <authentication mode=" ...

  10. openssl evp RSA 加密解密

    openssl evp RSA 加密解密 可以直接使用RSA.h 提供的接口 如下测试使用EVP提供的RSA接口 1. EVP提供的RSA 加密解密 主要接口: int EVP_PKEY_encryp ...