【题目】

  • 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. 一个简单的loading,纯属自娱自乐

    /// <reference path="/scripts/js/jquery.min.js" /> var zsw = { loading: function (im ...

  2. 《与小卡特一起学Python》 Code6 注释

    """这是一个包括多行的注释, 使用了三重引号字符串. 这不完全是注释,不过也可以相当于注释. """ #***************** ...

  3. 如何正确使用$_SERVER['DOCUMENT_ROOT']识别该路径的文件

    echo $_SERVER['DOCUMENT_ROOT']; 这时输出当前文件所在的路径 D:/phpStudy/WWW/study/php&mysql $_SERVER['DOCUMENT ...

  4. sql:sum(value)与count(letter),当用户不存在时查询到的值

    SELECT sum(value) FROM invoice where username='yueer' SELECT count(letters) FROM invoice where usern ...

  5. PHP基础知识之————PHP Web脚本中使用FFmpeg

    简介 本文将尝试指出在PHP Web脚本中使用FFmpeg时需要了解的所有重要事项.它还将显示一些使用示例,以使事情更清楚.这个想法也可以应用到其他web脚本语言. 从PHP脚本调用命令行工具 选择一 ...

  6. jsp中jstl标签的类似 if - else 语句 的语法

    在jsp中引入jstl的标签库和函数库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c&q ...

  7. Could not create the view: An unexpected exception was thrown.

    今天打开Myeclipse10的时候,发现server窗口出现一堆问题,问题如标题,然后下方出现了一堆java.lang.NullPointerException的问题. java.lang.Null ...

  8. Linux乱码问题解决方案

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  9. netperf安装及使用

    一.简介 Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf根据 ...

  10. UIautomator Python测试

    #!/usr/bin/env python # -*- coding: utf-8 -*- import unittest from mock import MagicMock, patch impo ...