当前位置:网站首页>LeetCode_ Stack_ Difficulties_ 394. string decoding
LeetCode_ Stack_ Difficulties_ 394. string decoding
2022-06-09 09:41:00 【I've been up and down in the Jianghu】
1. subject
Given an encoded string , Returns the decoded string .
The coding rule is : k[encoded_string], Represents the encoded_string Just repeat k Time . Be careful k Positive integer guaranteed .
You can think that the input string is always valid ; There are no extra spaces in the input string , And the square brackets entered always meet the format requirements .
Besides , You can assume that the raw data does not contain numbers , All numbers only indicate the number of repetitions k , For example, there is no such thing as 3a or 2[4] The input of .
Example 1:
Input :s = “3[a]2[bc]”
Output :“aaabcbc”
Example 2:
Input :s = “3[a2[c]]”
Output :“accaccacc”
Example 3:
Input :s = “2[abc]3[cd]ef”
Output :“abcabccdcdcdef”
Example 4:
Input :s = “abc3[cd]xyz”
Output :“abccdcdcdxyz”
Tips :
1 <= s.length <= 30
s By lowercase letters 、 Numbers and square brackets ‘[]’ form
s Guarantee is a It works The input of .
s The value range of all integers in is [1, 300]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/decode-string
2. Ideas
(1) Stack
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— Stack
public class Solution {
int index = 0;
public String decodeString(String s) {
// Use linked list to simulate stack
LinkedList<String> stack = new LinkedList<String>();
index = 0;
while (index < s.length()) {
char cur = s.charAt(index);
if (Character.isDigit(cur)) {
// Get a number k Parallel stack (1 ≤ k ≤ 300)
String digits = getDigits(s);
stack.addLast(digits);
} else if (Character.isLetter(cur) || cur == '[') {
// Get a letter and put it on the stack
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);
// Left bracket out of stack
stack.removeLast();
// At this time, the stack top element is the current sub The number of times the corresponding string should appear
int times = Integer.parseInt(stack.removeLast());
StringBuffer buffer = new StringBuffer();
String str = getString(sub);
// Construct string
while (times > 0) {
buffer.append(str);
times--;
}
// Add the constructed string to the stack
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();
}
// Splicing stack And returns
public String getString(LinkedList<String> stack) {
StringBuffer buffer = new StringBuffer();
for (String s : stack) {
buffer.append(s);
}
return buffer.toString();
}
}
边栏推荐
- [Android -- interview] the top ten platforms where programmers have joined w every month
- MySQL basic DML and DDL learning
- 使用Canvas画出多个多边形Polygon
- [5机器学习]全网最易懂的决策树(附源码)
- 如何看待 Dapr、Layotto 這種多運行時架構?
- MySQL基础 子查询练习
- Creation of menu for wechat applet development
- 2022-2028 global social finance industry research and trend analysis report
- SSM详解
- GLCC's first programming summer camp welcome to sign up for layotto, kusionstack, Nydus, Kata containers!
猜你喜欢

如何看待 Dapr、Layotto 这种多运行时架构?

【科技、商业和管理】看剧学创业:《硅谷》第六季第6-7集

ERP 系统,编译和学习

【Android -- 面试】程序员月入过 W 的十大平台

Kusionstack has a sense of open source | it took two years to break the dilemma of "separating lines like mountains"

Use a query statement to query the number of people whose data is within the score range of 0-60,60-80,80-100

【代码注释】doxygen

TS 泛型类和泛型接口的好处

Postman 接口压力测试

使用Canvas画出多个多边形Polygon
随机推荐
【1数据采集】数据爬虫的完整学习路径
根据投影坐标裁剪影像中的目标区域(附有完整代码)
面试官:如何秒开视频?什么是秒开视频?
Postman 接口压力测试
[redis learning 12] sentry mechanism of distributed cache, partitioned cluster
SSM details
NIO BIO AIO
最长公共子序列和最长公共子串
MySQL基础 子查询
【新手上路常见问答】如何用TensorFlow玩转深度学习?
Annexe 17 interprétation du programme réseau
2022-2028 global mobile phone jammer industry research and trend analysis report
Creation of menu for wechat applet development
MySQL基础 增删改查练习
neo4j访问浏览器时报错:ServiceUnavailable: WebSocket connection failure. Due to security constraints in your
MySQL basic functions
Minimum path sum
MySQL basic subquery
LeetCode_模拟_中等_621. 任务调度器
How do you view the multi runtime architecture of dapr and layotto?