当前位置:网站首页>LeetCode_栈_困难_394. 字符串解码
LeetCode_栈_困难_394. 字符串解码
2022-06-09 09:10:00 【一瓢江湖我沉浮】
1.题目
给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
示例 1:
输入:s = “3[a]2[bc]”
输出:“aaabcbc”
示例 2:
输入:s = “3[a2[c]]”
输出:“accaccacc”
示例 3:
输入:s = “2[abc]3[cd]ef”
输出:“abcabccdcdcdef”
示例 4:
输入:s = “abc3[cd]xyz”
输出:“abccdcdcdxyz”
提示:
1 <= s.length <= 30
s 由小写英文字母、数字和方括号 ‘[]’ 组成
s 保证是一个 有效 的输入。
s 中所有整数的取值范围为 [1, 300]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/decode-string
2.思路
(1)栈
思路参考本题官方题解。
3.代码实现(Java)
//思路1————栈
public class Solution {
int index = 0;
public String decodeString(String s) {
//使用链表来模拟栈
LinkedList<String> stack = new LinkedList<String>();
index = 0;
while (index < s.length()) {
char cur = s.charAt(index);
if (Character.isDigit(cur)) {
//获取一个数字 k 并进栈(1 ≤ k ≤ 300)
String digits = getDigits(s);
stack.addLast(digits);
} else if (Character.isLetter(cur) || cur == '[') {
//获取一个字母并进栈
stack.addLast(String.valueOf(s.charAt(index++)));
} else {
index++;
LinkedList<String> sub = new LinkedList<>();
while (!"[".equals(stack.peekLast())) {
sub.addLast(stack.removeLast());
}
Collections.reverse(sub);
//左括号出栈
stack.removeLast();
//此时栈顶元素为当前 sub 对应的字符串应该出现的次数
int times = Integer.parseInt(stack.removeLast());
StringBuffer buffer = new StringBuffer();
String str = getString(sub);
//构造字符串
while (times > 0) {
buffer.append(str);
times--;
}
//将构造好的字符串加入到栈中
stack.addLast(buffer.toString());
}
}
return getString(stack);
}
public String getDigits(String s) {
StringBuffer buffer = new StringBuffer();
while (Character.isDigit(s.charAt(index))) {
buffer.append(s.charAt(index++));
}
return buffer.toString();
}
//拼接 stack 中的所有字符串并返回
public String getString(LinkedList<String> stack) {
StringBuffer buffer = new StringBuffer();
for (String s : stack) {
buffer.append(s);
}
return buffer.toString();
}
}
边栏推荐
- Open application for "cloud native technology application and practice" demonstration course project in Colleges and Universities
- Android常见原理性面试题(初步整理)~
- Wechat applet - doodle Conference - conference release and my conference view
- English grammar_ adverb of manner
- [matlab] [digital simulation] [1] linprog solving linear programming, standard type
- MySQL basic functions
- MySQL基础 多表查询
- Codeworks round 797 (Div. 3) a~e problem solving record
- Can I LINQ a JSON- Can I LINQ a JSON?
- 2022-2028 global mobile phone jammer industry research and trend analysis report
猜你喜欢

MySQL基础 函数篇

附十七章 网络程序解读限定文章

QT开发--串口助手的编写

MySQL basic multi table query
![[texstudio] [3] relatively complete paper typesetting template and bib file reference method](/img/ba/160b236aaf0fc905609b8a70c7677d.jpg)
[texstudio] [3] relatively complete paper typesetting template and bib file reference method

微信小程序--嘟嘟会议--会议发布和我的会议查看

【代码注释】doxygen
![[Android -- interview] the top ten platforms where programmers have joined w every month](/img/55/7fc88963a55f8b88672c9b285842a2.png)
[Android -- interview] the top ten platforms where programmers have joined w every month

50% cost savings, 9-person team develops WOLAI online document application using function calculation

Interviewer: how to open a video? What is second on video?
随机推荐
项目面试题
[texstudio] [1] fractional dfrac, tfrac undefined control square error
微信小程序获取用户信息并更新
【Redis学习12】分布式缓存之哨兵机制,分片集群
Android常见原理性面试题(初步整理)~
MySQL basic multi table query
English grammar_ adverb
中位数图(前缀和)
[redis learning 13] redis builds master-slave cluster, sentinel cluster and partition cluster
Learn about graph database neo4j (I)
【计算机网络-19】计算机网络面试题
了解图数据库neo4j(三)
[texstudio] [3] relatively complete paper typesetting template and bib file reference method
MySQL基础 函数篇
Database problems MySQL
[texstudio] [2] general picture and table presentation
MySQL uses while to batch insert data in stored procedures (performance difference between batch submission and single submission)
three.js学习笔记(十六)——汹涌的海洋
如何看待 Dapr、Layotto 这种多运行时架构?
Que pensez - vous des architectures Multi - temps comme DAPR et layotto?