LC 394. Decode String
问题描述
Given an encoded string, return its 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) {
int pos = ;
return foo(pos, s);
}
string foo(int& pos, string s){
int num = ;
string word = "";
for(;pos<s.size();++pos){ // 3[ab] = ababab
char cur = s[pos];
if(cur >='' && cur <= ''){
num = num * + cur - ''; // 计算出倍数
}else if(cur == ']'){
return word; // 结束
}else if(cur == '['){
string curStr = foo(++pos, s); // 如果遇到 [ ,将 pos 向后移动一位
for(;num>;num--) word += curStr; // 上一行获得了curStr,而num是上一个循环准备好了,所以可以直接使用。
}else{
word += cur; // 这个是为了应对平常的 char -> return curStr
}
}
return word;
}
};
额外说明
灵魂
这个答案的灵魂,在于当 s[pos] == “[” 的时候,foo( ++pos, s)。
数字,代表×的倍率,一定会出现的。
[ ,代表会出现要处理的字符串,因此这一栏有 string curStr,并且由于有 num 了,所以处理拼合好的字符,也是在这里进行处理的。
],代表着所有递归的结束,不论是大循环,还是小循环,返回 word。
else,也是灵魂,一定意味着normal character,所以直接附在word后面即可。等遇到了 ] ,直接返回,交给 刚才的 ] 里面的 for 处理。
数字,从string到int
int num = 0;
for cur in string:
num = num * 10 + cur - '0';
LC 394. Decode String的更多相关文章
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394. Decode String 解码icc字符串3[i2[c]]
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- 【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- Python 解LeetCode:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
随机推荐
- GoEasy实现websocket 推送消息通知到客户端
最近在实现一个推送功能,用户扫描二维码签到,后台及时将签到成功信息推送到浏览器端.排除了前端ajax轮询的方式,决定采用websocket及时推送. 于是发现了第三方websocket推送库GoEas ...
- 前端逼死强迫症之css续集
上节内容回顾 如果点击图片调转到连接,a标签下套img标签,在IE有的版本中,会有蓝色边框. 因为a标签默认字体颜色就是蓝色,img标签继承了父级标签,而IE浏览器默认给边框加了宽度.解决: < ...
- scrapy框架之log日志
scrapy中的debug信息 在scrapy中设置log 1.在settings中设置log级别,在settings.py中添加一行: Scrapy提供5层logging级别: CRITICAL - ...
- Ryu控制器安装部署和入门
Ryu官网简介,原滋原味 Ryu is a component-based software defined networking framework. Ryu provides software c ...
- Embedded based learning
简单整理了一些嵌入式底层需要接触的相关概念. # CPU CU. Control Unit. send need-clac-data -> ALU clac -> get resul ...
- Python全国二级等级考试(2019)
一.前言 2018年9月随着全国计算机等级考试科目中加入“二级Python”,也确立了Python在国内的地位,猪哥相信Python语言势必会像PS那般普及.不久的将来,谁会Python谁就能获得女神 ...
- Gevent模块
前言 如何在使用1个线程的前提下,提网站的并发性,使用协程? 如果要使用协程首先要解决2个问题: 1.如何检测到代码中遇到了IO操作?(XX) 2.如何在线程代码里上下切换?(Greelet模块) 而 ...
- OOM异常的发生原因
一,jvm内存区域 1,程序计数器 一块很小的内存空间,作用是当前线程所执行的字节码的行号指示器. 2,java栈 与程序计数器一样,java栈(虚拟机栈)也是线程私有的,其生命周期与线程相同.通常存 ...
- train_faster_rcnn.sh
#!/bin/bash set -x set -e export PYTHONUNBUFFERED="True" GPU_ID=$1 DATASET=$2 NET=$3 array ...
- 03Flutter仿京东商城项目 封装适配库以及实现左右滑动ListView
ScreenAdaper.dart import 'package:flutter_screenutil/flutter_screenutil.dart'; class ScreenAdaper { ...