diff --git a/Algorithms/0385. Mini Parser/385. Mini Parser.go b/Algorithms/0385. Mini Parser/385. Mini Parser.go new file mode 100644 index 00000000..ffbd1818 --- /dev/null +++ b/Algorithms/0385. Mini Parser/385. Mini Parser.go @@ -0,0 +1,117 @@ +package leetcode + +import ( + "fmt" + "strconv" +) + +/** + * // This is the interface that allows for creating nested lists. + * // You should not implement it, or speculate about its implementation + * type NestedInteger struct { + * } + * + * // Return true if this NestedInteger holds a single integer, rather than a nested list. + * func (n NestedInteger) IsInteger() bool {} + * + * // Return the single integer that this NestedInteger holds, if it holds a single integer + * // The result is undefined if this NestedInteger holds a nested list + * // So before calling this method, you should have a check + * func (n NestedInteger) GetInteger() int {} + * + * // Set this NestedInteger to hold a single integer. + * func (n *NestedInteger) SetInteger(value int) {} + * + * // Set this NestedInteger to hold a nested list and adds a nested integer to it. + * func (n *NestedInteger) Add(elem NestedInteger) {} + * + * // Return the nested list that this NestedInteger holds, if it holds a nested list + * // The list length is zero if this NestedInteger holds a single integer + * // You can access NestedInteger's List element directly if you want to modify it + * func (n NestedInteger) GetList() []*NestedInteger {} + */ + +// NestedInteger define +type NestedInteger struct { + Num int + List []*NestedInteger +} + +// IsInteger define +func (n NestedInteger) IsInteger() bool { + if n.List == nil { + return true + } + return false +} + +// GetInteger define +func (n NestedInteger) GetInteger() int { + return n.Num +} + +// SetInteger define +func (n *NestedInteger) SetInteger(value int) { + n.Num = value +} + +// Add define +func (n *NestedInteger) Add(elem NestedInteger) { + n.List = append(n.List, &elem) +} + +// GetList define +func (n NestedInteger) GetList() []*NestedInteger { + return n.List +} + +// Print define +func (n NestedInteger) Print() { + if len(n.List) != 0 { + for _, v := range n.List { + if len(v.List) != 0 { + v.Print() + return + } + fmt.Printf("%v ", v.Num) + } + } else { + fmt.Printf("%v ", n.Num) + } + fmt.Printf("\n") +} + +func deserialize(s string) *NestedInteger { + stack, cur := []*NestedInteger{}, &NestedInteger{} + for i := 0; i < len(s); { + switch { + case isDigital(s[i]) || s[i] == '-': + j := 0 + for j = i + 1; j < len(s) && isDigital(s[j]); j++ { + } + num, _ := strconv.Atoi(s[i:j]) + next := &NestedInteger{} + next.SetInteger(num) + if len(stack) > 0 { + stack[len(stack)-1].List = append(stack[len(stack)-1].GetList(), next) + } else { + cur = next + } + i = j + case s[i] == '[': + next := &NestedInteger{} + if len(stack) > 0 { + stack[len(stack)-1].List = append(stack[len(stack)-1].GetList(), next) + } + stack = append(stack, next) + i++ + case s[i] == ']': + cur = stack[len(stack)-1] + stack = stack[:len(stack)-1] + i++ + case s[i] == ',': + i++ + } + } + return cur +} diff --git a/Algorithms/0385. Mini Parser/385. Mini Parser_test.go b/Algorithms/0385. Mini Parser/385. Mini Parser_test.go new file mode 100644 index 00000000..8ca47fb5 --- /dev/null +++ b/Algorithms/0385. Mini Parser/385. Mini Parser_test.go @@ -0,0 +1,64 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question385 struct { + para385 + ans385 +} + +// para 是参数 +// one 代表第一个参数 +type para385 struct { + n string +} + +// ans 是答案 +// one 代表第一个答案 +type ans385 struct { + one []int +} + +func Test_Problem385(t *testing.T) { + + qs := []question385{ + + question385{ + para385{"[[]]"}, + ans385{[]int{}}, + }, + + question385{ + para385{"[]"}, + ans385{[]int{}}, + }, + + question385{ + para385{"[-1]"}, + ans385{[]int{-1}}, + }, + + question385{ + para385{"[123,[456,[789]]]"}, + ans385{[]int{123, 456, 789}}, + }, + + question385{ + para385{"324"}, + ans385{[]int{324}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 385------------------------\n") + + for _, q := range qs { + _, p := q.ans385, q.para385 + fmt.Printf("【input】:%v 【output】: \n", p) + fmt.Printf("NestedInteger = ") + deserialize(p.n).Print() + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0385. Mini Parser/README.md b/Algorithms/0385. Mini Parser/README.md new file mode 100755 index 00000000..20c810ca --- /dev/null +++ b/Algorithms/0385. Mini Parser/README.md @@ -0,0 +1,50 @@ +# [385. Mini Parser](https://leetcode.com/problems/mini-parser/) + + +## 题目: + +Given a nested list of integers represented as a string, implement a parser to deserialize it. + +Each element is either an integer, or a list -- whose elements may also be integers or other lists. + +**Note:** You may assume that the string is well-formed: + +- String is non-empty. +- String does not contain white spaces. +- String contains only digits `0-9`, `[`, `-` `,`, `]`. + +**Example 1:** + + Given s = "324", + + You should return a NestedInteger object which contains a single integer 324. + +**Example 2:** + + Given s = "[123,[456,[789]]]", + + Return a NestedInteger object containing a nested list with 2 elements: + + 1. An integer containing value 123. + 2. A nested list containing two elements: + i. An integer containing value 456. + ii. A nested list with one element: + a. An integer containing value 789. + + +## 题目大意 + +给定一个用字符串表示的整数的嵌套列表,实现一个解析它的语法分析器。列表中的每个元素只可能是整数或整数嵌套列表 + +提示:你可以假定这些字符串都是格式良好的: + +- 字符串非空 +- 字符串不包含空格 +- 字符串只包含数字0-9, [, - ,, ] + + + +## 解题思路 + +- 将一个嵌套的数据结构中的数字转换成 NestedInteger 数据结构。 +- 这一题用栈一层一层的处理就行。有一些比较坑的特殊的边界数据见测试文件。这一题正确率比很多 Hard 题还要低的原因应该是没有理解好题目和边界测试数据没有考虑到。NestedInteger 这个数据结构笔者实现了一遍,见代码。 diff --git a/README.md b/README.md index c1e93cc3..e26475bd 100644 --- a/README.md +++ b/README.md @@ -448,7 +448,7 @@ | 0382 | Linked List Random Node | | 49.40% | Medium | | | 0383 | Ransom Note | | 50.10% | Easy | | | 0384 | Shuffle an Array | | 50.30% | Medium | | -| 0385 | Mini Parser | | 31.90% | Medium | | +| 0385 | Mini Parser | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0385.%20Mini%20Parser) | 31.90% | Medium | | | 0386 | Lexicographical Numbers | | 46.50% | Medium | | | 0387 | First Unique Character in a String |[Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0387.%20First%20Unique%20Character%20in%20a%20String) | 50.20% | Easy | | | 0388 | Longest Absolute File Path | | 39.30% | Medium | |