当前位置:网站首页>Force deduction solution summary 358 Mini parser
Force deduction solution summary 358 Mini parser
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link :
describe :
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
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * This symmetrical structure is most suitable for using the idea of stack to answer questions . * Because in this question , Objects have two forms , Arrays and numbers . So our husband joined the collection as an object , Its type will be determined in the subsequent traversal process . * Stack is used to load layer by layer objects , Read [ It means adding one level . Read ] Then introduce a level . * Traverse from front to back for characters , Each character is divided into the following situations : * 1. String is '[' when , It indicates that the current object is in the form of an array , Then an object is generated and added to the array ( When it is not determined at this time, the array is still in digital form ). * 2. String is ']' when , It indicates that the loop of the current array ends , From the stack pop Out of the top node . * 3. String is '.' when , Then it indicates that the next set of objects should be read . * 4. When the string is a number , Then record ,start++. besides , Except for the number type , Will trigger the end of string judgment , Generate a numeric object and add it to the top stack object .
Code :
public class Solution385 {
public NestedInteger deserialize(String s) {
Stack<NestedInteger> stack = new Stack<>();
NestedInteger root = new NestedInteger();
stack.add(root);
StringBuilder builder = new StringBuilder();
int start = 0;
char[] chars = s.toCharArray();
while (true) {
if (start == chars.length) {
addValue(builder, stack);
break;
}
char aChar = chars[start];
if (aChar == '[') {
NestedInteger value = new NestedInteger();
stack.peek().getList().add(value);
stack.add(value);
start++;
continue;
}
if (aChar == ']') {
addValue(builder, stack);
stack.pop();
start++;
continue;
}
if (aChar == ',') {
addValue(builder, stack);
start++;
continue;
}
builder.append(aChar);
start++;
}
NestedInteger result = stack.pop().getList().get(0);
return result;
}
private void addValue(StringBuilder builder, Stack<NestedInteger> stack) {
if (builder.length() == 0) {
return;
}
NestedInteger value = new NestedInteger(Integer.parseInt(builder.toString()));
stack.peek().add(value);
builder.setLength(0);
}
class NestedInteger {
Integer mValue;
List<NestedInteger> list = new ArrayList<>();
public NestedInteger() {
}
public NestedInteger(int value) {
this.mValue = value;
}
public boolean isInteger() {
return mValue != null;
}
public Integer getInteger() {
return mValue;
}
public void setInteger(Integer value) {
this.mValue = value;
this.list = null;
}
public void add(NestedInteger ni) {
list.add(ni);
}
public List<NestedInteger> getList() {
return list;
}
}
}边栏推荐
- Operating mechanism of Google ads bidding
- Wide match modifier symbol has been deprecated, do not use
- Advantages of Google ads
- Installing mysql-5.7 for Linux (centos7)
- The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
- [adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology
- 力扣解法汇总732-我的日程安排表 III
- Leetcode 45 jump game II
- Leetcode 55 jump game
- UE4\UE5触摸屏touch事件:单指、双指
猜你喜欢

How to maximize the use of various matching methods—— Google SEM

Introduction to SVM

MySQL advanced knowledge points

Explore performance optimization! Performance improvement from 2 months to 4 hours!

BaseDexClassLoader那些事

超图倾斜数据合并根节点后转3dtiles

Three main factors determining advertising quality

php开发 博客系统的公告模块的建立和引入

ACL 2022 | 预训练语言模型和图文模型的强强联合

Glfwpollevents() program crash
随机推荐
力扣解法汇总713- 乘积小于 K 的子数组
xcall 集群脚本(查看jps命令)
竞价广告每次点击出价多少钱是固定的吗?
php安全开发 13博客系统的栏目模块的编写
[adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
Is the bidding price fixed for each click?
力扣解法汇总427-建立四叉树
力扣解法汇总824-山羊拉丁文
Wide match modifier symbol has been deprecated, do not use
通过搜索广告附加信息让广告更具相关性
2022最全面的Redis事务控制(带图讲解)
力扣解法汇总732-我的日程安排表 III
Fatal error in launcher: unable to create process using
力扣解法汇总675-为高尔夫比赛砍树
The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
Bracket generation (backtracking)
Basedexclassloader
php开发 博客系统的公告模块的建立和引入
Explore performance optimization! Performance improvement from 2 months to 4 hours!
关于vagrant up的一个终结之谜