当前位置:网站首页>Leetcode 394. string decoding
Leetcode 394. string decoding
2022-07-22 20:12:00 【henujolly】
class Solution {
public:
string decodeString(string s) {
string res = "";
stack <int> nums;
stack <string> strs;
int num = 0;
int len = s.size();
for(int i = 0; i < len; ++ i)
{
if(s[i] >= '0' && s[i] <= '9')
{
num = num * 10 + s[i] - '0';
}
else if((s[i] >= 'a' && s[i] <= 'z') ||(s[i] >= 'A' && s[i] <= 'Z'))
{
res = res + s[i];
}
else if(s[i] == '[') // take ‘[’ Press the number before into nums Inside the stack , The letter string is pressed into strs Inside the stack
{
nums.push(num);
num = 0;
strs.push(res);
res = "";
}
else // encounter ‘]’ when , Operate the matching ‘[’ Characters between , Use the law of distribution
{
int times = nums.top();
nums.pop();
for(int j = 0; j < times; ++ j)
strs.top() += res;
res = strs.top(); // Then if it's still a letter , Will be added directly to res after , Because they are the same level of operations
// If it's left bracket ,res Will be pressed into strs Stack , As the operation of the upper layer
strs.pop();
}
}
return res;
}
};
author :youlookdeliciousc
link :https://leetcode-cn.com/problems/decode-string/solution/ti-jie-czhan-by-youlookdeliciousc/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
猜你喜欢
Neo4j安装教程
Spark SQL built-in functions and custom functions UDF
Simple implementation process of CNN
Host Ping fails in VMware NAT mode virtual machine: different from most methods
System table space is full
Spark loads CSV and JSON files (attached to the virtual machine to execute the jar package)
Microblaze添加自定义IP核,挂AXI总线实现SSD1306 OELD驱动
Oracle 11g installs and starts EM based on centos7
Development and construction of NFT card chain game system DAPP
Xilinx FPGA Microblaze AXI_IIC使用方法及心得
随机推荐
数组push时 覆盖的问题
【数据库基础干货】MySQL基础及慢查询优化实践
appstore 上传屏幕快照尺寸
为什么memset不能将数组元素初始化为1?
Summary of MySQL grant user permissions
NFT卡牌链游系统Dapp开发搭建
Appstore upload screenshot size
Installation of mysql5.7 (yum, binary, compile and install)
Fluent 2 Advanced (VI): use of callback functions
NC54 三数之和
leetcode 394. 字符串解码
NC26 括号生成
If else optimization
Liangning product course learning
Youboxun helps Shenzhen build a global "city of Hongmeng Oula"
RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
Flutter 2进阶(六):回调函数的使用
Kotlin学习三:页面的常用写法
[online case analysis] one-time slow query optimization and summary thinking
开幕在即 | “万物互联,使能千行百业”2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛