Question

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".

Solution

用两个栈实现。

Code

class Solution {
public:
string decodeString(string s) {
string res = "";
for (int i = 0; i < s.length(); i++) {
if ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '[') {
// 10[leetcode]ef, 处理ef
if (nums.empty()) {
res += s[i];
} else
letters.push(s[i]);
} if (s[i] >= '0' && s[i] <= '9') {
string tmp = "";
tmp += s[i];
// 处理多个连续的数字
for (int j = i + 1; j < s.length(); j++) {
if (s[j] >= '0' && s[j] <= '9') {
tmp += s[j];
i++;
}
else
break;
} stringstream ss;
ss << tmp;
int value;
ss >> value;
nums.push(value);
}
if (s[i] == ']') {
int value = nums.top();
nums.pop();
string tmp = "";
while (1) {
char c = letters.top();
if (c != '[')
tmp += c;
letters.pop();
if (c == '[')
break;
}
reverse(tmp.begin(), tmp.end());
string tmp2 = "";
for (int i = 0; i < value; i++) {
tmp2 += tmp;
}
// 如果为空了,就直接累加到结果里,否则压入栈中
if (nums.empty()) {
res += tmp2;
} else {
for (int i = 0; i < tmp2.length(); i++)
letters.push(tmp2[i]);
}
}
}
return res;
}
stack<int> nums;
stack<char> letters;
};

LeetCode——Decode String的更多相关文章

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

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

  2. [LeetCode] Decode String 题解

    题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...

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

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

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

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

  5. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  6. [LeetCode] Decode Ways 解题思路

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  8. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  9. Leetcode -- 394. Decode String

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

随机推荐

  1. http 服务器编程 适配器

    小结: 1.HandleFunc 只是一个适配器 go http 服务器编程(1) - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1 ...

  2. PXE,ipmi,bare metal

    IPMI(Intelligent Platform Management Interface)是一个智能平台管理接口. 用户可以利用IPMI 监视服务器等设备的物理特征,如各部件的温度.电压.风扇工作 ...

  3. 网络编程 - socket通信/粘包/文件传输/udp - 总结

    socket通信 1.简单的套接字通信 import socket phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.bin ...

  4. css冲突2 要关闭的css在项目代码以外,但是是通过<link>标签引入的css(例如bootstrap):解决方法,在APP.css中使用全局样式

    css冲突,导致html字体过小. 通过浏览器检查发现,导致字体过小的css来自bootstrap. 现要关闭bootstrap的css: 直接在APP.css中添加: html{ font-size ...

  5. Period II---fzu1901(Next数组)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 给你一个字符串 s 求出所有满足s[i] == s[i+p] ( 0 < i+p < len ...

  6. Linux命令(1)——xargs命令

    1.功能: xargs能够将stdin中以空格或换行符进行分隔的数据.形成以空格分隔的參数(arguments),传递给其它命令.由于以空格作为分隔符,所以有一些文件名称或者其它意义的名词内含有空格的 ...

  7. jquery Chosen使用

    1,首先去http://harvesthq.github.io/chosen/下载插件. 2,在网页中加入下面的文件. <link rel="stylesheet" href ...

  8. uib-datepicker-popup 日期不可输入 只可以选择 :readonly="true"

    <div name="{{property.name}}" style="display: flex" ng-switch-when="date ...

  9. #运算符、不同的指针类型、数组和指针、指针运算、堆、栈、静态区、只读区、下标VS指针

    #运算符:用于在预编译期将宏参数转换为字符串 #define CONVERS(x)  #x   //注:没用双引号包括. 不同类型的指针占用的内存空间大小相同. 局部变量 定义: a[5]; 打印a[ ...

  10. MySQL整理(二)

    一.MySQL操作表的约束 MySQL提供了一系列机制来检查数据库表中的数据是否满足规定条件,以此来保证数据库表中数据的准确性和一致性,这种机制就是约束. (1)设置非空约束(NOT NULL),唯一 ...