当前位置:网站首页>Using stack to solve the problem of "mini parser"
Using stack to solve the problem of "mini parser"
2022-04-23 03:05:00 【& Xiaobai &】
13、 ... and 、 Mini parser
13.1、 Question design requirements
Given a string s Represents a nested list of integers , Implement a parser that parses it and returns the parsed results NestedInteger .
Each element in the list can only be an integer or an integer nested list
Example 1:
Input :s = "324",
Output :324
explain : You should return one NestedInteger object , Only integer values are included 324.
Example 2:
Input :s = "[123,[456,[789]]]",
Output :[123,[456,[789]]]
explain : Return to one NestedInteger Object contains a nested list of two elements :
1. One integer Contains the value 123
2. A nested list of two elements :
i. One integer Contains the value 456
ii. A nested list containing one element
a. One integer Contains the value 789
Tips :
1 <= s.length <= 5 * 104
s By digital 、 square brackets "[]"、 Minus sign '-' 、 comma ',' form
Use cases guarantee s It's analyzable NestedInteger
The range of all values in the input is [-106, 106]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/mini-parser
13.2、 Their thinking
Traverse from left to right s, If you encounter “[” , It means a new NestedInteger example , You need to put it on the stack . If you encounter “]” or “,” , It means a number or NestedInteger End of instance , You need to add it to the top of the stack NestedInteger example . Finally, return the instance at the top of the stack .
13.3、 Algorithm
/** * // This is an interface that allows you to create embedded lists * // You shouldn't realize it , Nor should we speculate about its implementation * public interface NestedInteger { * // Constructor initializes an empty nested list * public NestedInteger(); * * // Constructor initializes a single integer * public NestedInteger(int value); * * // If so NestedInteger Contains a single integer , Instead of nested lists , Then return to True * public boolean isInteger(); * * // If NestedInteger Contains a single integer , Return this NestedInteger Saved single integer * // If so NestedInteger Contains nested lists , Then return to NULL * public Integer getInteger(); * * // Put this NestedInteger Set to include a single integer * public void setInteger(int value); * * // Set this NestedInteger To save the nested list and add nested integers to it * public void add(NestedInteger ni); * * // If NestedInteger Contains nested lists , Return this NestedInteger Saved nested list * // If so NestedInteger Contains a single integer , Then return to the empty list * public List<NestedInteger> getList(); * } */
//parseInt() function : Used to parse a string , And return an integer
//isDigit() function : Check whether the parameter is decimal numeric character
class Solution {
// Traverse from left to right s, If you encounter "[", It means a new NestedInteger example , You need to put it on the stack .
// If you encounter "]" or ",", It means a number or NestedInteger End of instance , You need to add it to the top of the stack NestedInteger example .
// Finally, return the instance at the top of the stack .
public NestedInteger deserialize(String s) {
// No, "[", Just an integer , end
if(s.charAt(0) != '['){
return new NestedInteger(Integer.parseInt(s));
}
// Define a stack to store data
Deque<NestedInteger> stack = new ArrayDeque<>();
int num = 0;
boolean negative = false;
for (int i = 0; i < s.length(); i++) {
// Take out the characters in the string one by one
char c = s.charAt(i);
// The character is '-', Output true
if (c == '-') {
negative = true;
}else if (Character.isDigit(c)){
// If the character is a number , Pressure into the stack
num = num * 10 + c - '0';
}else if (c == '['){
// If the character is '[', Pressure into the stack
stack.push(new NestedInteger());
}else if (c == ',' || c == ']'){
// If you encounter ']' or ',', Put numbers or NestedInteger The instance is added to the top of the stack NestedInteger example
if (Character.isDigit(s.charAt(i-1))){
if (negative){
num *= -1;
}
stack.peek().add(new NestedInteger(num));
}
num = 0;
negative = false;
if (c == ']' && stack.size() > 1){
NestedInteger ni = stack.pop();
stack.peek().add(ni);
}
}
}
return stack.pop();
}
}
版权声明
本文为[& Xiaobai &]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301476792.html
边栏推荐
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- 中后二叉建树
- 利用正反遍历来解决“字符的最短距离”问题
- C# 读写二进制文件
- Encapsulation of ele table
- VirtualBox virtual machine (Oracle VM)
- In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
- Blazor University (12)组件 — 组件生命周期
- Numpy stack function
- Openfeign timeout setting
猜你喜欢

TP5 customization in extend directory succeeded and failed. Return information

MAUI初体验:爽

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!

It turns out that PID was born in the struggle between Lao wangtou and Lao sky

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)

Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang

ASP.NET 6 中间件系列 - 自定义中间件类

Service avalanche effect

利用栈的回溯来解决“文件的最长绝对路径”问题

PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
随机推荐
FileNotFoundError: [Errno 2] No such file or directory
使用split来解决“最常见的单词”问题
TP5 inherits base and uses the variables in base
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Detailed explanation of distributed things
L2-006 树的遍历(中后序确定二叉树&层序遍历)
Reverse a linked list < difficulty coefficient >
VirtualBox virtual machine (Oracle VM)
AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
Array and collection types passed by openfeign parameters
准备一个月去参加ACM,是一种什么体验?
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
对.NET未来的一点感悟
Encapsulation of ele table
Onenet connection process
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
微软是如何解决 PC 端程序多开问题的
Systemctl start Prometheus + grafana environment
Summary of interface automation interview questions for software testing