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. SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)

    import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import com.opensymphony.x ...

  2. 并发编程 - 进程 - 1.互斥锁/2.模拟抢票/3.互斥锁与join区别

    1.互斥锁: 互斥锁:Lock 原理就是把并发变成串行,一个一个运行,不错乱,但效率低 保证多个进程修改一块数据时,大家是一个一个修改,不错乱 mutex.acquire() mutex.releas ...

  3. Unmet dependencies. Try 'apt-get -f install' with no packages

    在ubuntu14.04上用sudo apt-get install percona-xtrabackup安装xtrabackup时提示 zhj@my-SERVER:~$ sudo apt-get i ...

  4. mmu介绍

    arm exynos4412平台mmu配置以及的简单创建. 1.mmu的全称是Memory Management Unit(内存管理单元) 2.mmu所在位置.mmu再arm核心.他不是一个外设,他是 ...

  5. 常用的自定义Python函数

    常用的自定义Python函数 1.时间戳转为日期字串,精确到ms.单位s def timestamp2datems(timestamp): ''' 时间戳转为日期字串,精确到ms.单位s :param ...

  6. SharePoint 2013 附加内容数据库后出现404错误

    本文讲述怎样解决SharePoint 2013 加内容数据库(Content DataBase)后出现404错误. 笔者依照http://technet.microsoft.com/en-us/lib ...

  7. zabbix 添加 微信、邮件 媒介详解

    1:zabbix 添加 微信.邮件 媒介.  1.2:发送告警邮件         1:一次完整的监控流程可以简单描述为:         Host Groups (设备组) ->Hosts ( ...

  8. 使用python操作文件实现购物车程序

    使用python操作文件实现购物车程序 题目要求如下: 实现思路 始终维护一张字典,该字典里保存有用户账号密码,购物车记录等信息.在程序开始的时候读进来,程序结束的时候写回文件里去.在登录注册的部分, ...

  9. Python遇到SyntaxError: Non-ASCII character '\xe5' in file D:\eclipseworkspace\test\test_urllib2.py on line2

    写Python时遇到SyntaxError: Non-ASCII character '\xe5' in file D:\eclipseworkspace\test\test_urllib2.py o ...

  10. CCTV-2《遇见大咖》专访雷军----笔记记录

    与央视记者约好两点采访,但因为公司会议拖到了三点.雷军对此表示抱歉,解释了一天的行程,并说:“今天不算密的,密的平均一天应该有十一个会.然后我现在基本每天中午饭,大概就是三分钟的时间.” 因为要上镜, ...