LeetCode——Decode String
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的更多相关文章
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] Decode String 题解
题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...
- [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) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
随机推荐
- python基础-第七篇-7.4异常处理
异常基础: 异常处理首先要捕获异常,不让程序中断,也不让错误信息直接呈现出来,然后就是你该怎么处理异常,以什么方式显示 try: pass except Exception,ex: pass 在需要用 ...
- 剑指Offer——合并两个排序的链表
题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 分析: 苦力活,使用两个指针分别指向未被合并的两个链表的首部,比较两个首部数值的大小,合并数值 ...
- 【opencv入门篇】快速在VS上配置opencv
环境配置:win7-32 + opencv2.4.6 + vs2013 注意:无论电脑是32位还是64位,配置opencv库目录时选择x84文件夹!因为编译都是使用32位编译:如果选用X64,则程序运 ...
- JavaWeb-HttpServletResponse对象一
web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象.resquest和response对象既然代表请求和响应,那么要 ...
- git提交到远程虚拟机
git到自己的虚拟机中第一步:打通git(一)Linux中(ip为10.1.8.1)1.安装git如:Ubuntu中安装gitapt install git 2.Ubuntu中添加git用户sudo ...
- Jmeter(十)Linux下配置安装Jmeter及执行测试任务
一.安装JDK7.0版本 1.先卸载服务器自带的jdk软件包 # java -version #查看服务器是否安装过 # rpm -qa |grep gcj #查看服务器安装的jdk软件包信息 # y ...
- HDevelop数据类型
*图形类型*图像Hwnd:=3600read_image(Image, 'fabrik')disp_obj(Image, Hwnd)*region 区域gen_rectangle1(Rectangle ...
- ios开发中怎么获取应用崩溃日志
如何获得crash日志 当一个iOS应用程序崩溃时,系统会创建一份crash日志保存在设备上.crash日志记录着应用程序崩溃信息,通常包含着每个执行线程的栈调用信息(低内存闪退日志例外),对于开发人 ...
- Selenium的PageFactory & PageObject 在大型项目中的应用
因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手动的操作步骤去转换,近日看到一个文章,又去wiki上查了查,觉 ...
- undefined reference to _imp__xmlFree
Re: [xml] MSYS and MINGW: undefined reference to _imp__xmlFree From: Mike Peat <mpeat unicorninte ...