|
1 | 1 | __________________________________________________________________________________________________
|
| 2 | +sample 32 ms submission |
| 3 | +# """ |
| 4 | +# This is the interface that allows for creating nested lists. |
| 5 | +# You should not implement it, or speculate about its implementation |
| 6 | +# """ |
| 7 | +#class NestedInteger: |
| 8 | +# def __init__(self, value=None): |
| 9 | +# """ |
| 10 | +# If value is not specified, initializes an empty list. |
| 11 | +# Otherwise initializes a single integer equal to value. |
| 12 | +# """ |
2 | 13 |
|
| 14 | +# def isInteger(self): |
| 15 | +# """ |
| 16 | +# @return True if this NestedInteger holds a single integer, rather than a nested list. |
| 17 | +# :rtype bool |
| 18 | +# """ |
| 19 | + |
| 20 | +# def add(self, elem): |
| 21 | +# """ |
| 22 | +# Set this NestedInteger to hold a nested list and adds a nested integer elem to it. |
| 23 | +# :rtype void |
| 24 | +# """ |
| 25 | + |
| 26 | +# def setInteger(self, value): |
| 27 | +# """ |
| 28 | +# Set this NestedInteger to hold a single integer equal to value. |
| 29 | +# :rtype void |
| 30 | +# """ |
| 31 | + |
| 32 | +# def getInteger(self): |
| 33 | +# """ |
| 34 | +# @return the single integer that this NestedInteger holds, if it holds a single integer |
| 35 | +# Return None if this NestedInteger holds a nested list |
| 36 | +# :rtype int |
| 37 | +# """ |
| 38 | + |
| 39 | +# def getList(self): |
| 40 | +# """ |
| 41 | +# @return the nested list that this NestedInteger holds, if it holds a nested list |
| 42 | +# Return None if this NestedInteger holds a single integer |
| 43 | +# :rtype List[NestedInteger] |
| 44 | +# """ |
| 45 | + |
| 46 | +class Solution: |
| 47 | + def deserialize(self, s: str) -> NestedInteger: |
| 48 | + return NestedInteger(s) |
3 | 49 | __________________________________________________________________________________________________
|
| 50 | +sample 16488 kb submission |
| 51 | +# """ |
| 52 | +# This is the interface that allows for creating nested lists. |
| 53 | +# You should not implement it, or speculate about its implementation |
| 54 | +# """ |
| 55 | +#class NestedInteger: |
| 56 | +# def __init__(self, value=None): |
| 57 | +# """ |
| 58 | +# If value is not specified, initializes an empty list. |
| 59 | +# Otherwise initializes a single integer equal to value. |
| 60 | +# """ |
| 61 | +# |
| 62 | +# def isInteger(self): |
| 63 | +# """ |
| 64 | +# @return True if this NestedInteger holds a single integer, rather than a nested list. |
| 65 | +# :rtype bool |
| 66 | +# """ |
| 67 | +# |
| 68 | +# def add(self, elem): |
| 69 | +# """ |
| 70 | +# Set this NestedInteger to hold a nested list and adds a nested integer elem to it. |
| 71 | +# :rtype void |
| 72 | +# """ |
| 73 | +# |
| 74 | +# def setInteger(self, value): |
| 75 | +# """ |
| 76 | +# Set this NestedInteger to hold a single integer equal to value. |
| 77 | +# :rtype void |
| 78 | +# """ |
| 79 | +# |
| 80 | +# def getInteger(self): |
| 81 | +# """ |
| 82 | +# @return the single integer that this NestedInteger holds, if it holds a single integer |
| 83 | +# Return None if this NestedInteger holds a nested list |
| 84 | +# :rtype int |
| 85 | +# """ |
| 86 | +# |
| 87 | +# def getList(self): |
| 88 | +# """ |
| 89 | +# @return the nested list that this NestedInteger holds, if it holds a nested list |
| 90 | +# Return None if this NestedInteger holds a single integer |
| 91 | +# :rtype List[NestedInteger] |
| 92 | +# """ |
| 93 | + |
| 94 | +class Solution: |
| 95 | + def deserialize(self, s: str) -> NestedInteger: |
| 96 | + nestedArray = [NestedInteger()] |
| 97 | + startIndex = None |
| 98 | + for idx, char in enumerate(s): |
| 99 | + if char == '[': |
| 100 | + nestedArray.append(NestedInteger()) |
| 101 | + elif char == ']': |
| 102 | + if startIndex != None: |
| 103 | + nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx]))) |
| 104 | + startIndex = None |
| 105 | + nestedArray[-2].add(nestedArray.pop()) |
| 106 | + elif char == ',': |
| 107 | + if startIndex != None: |
| 108 | + nestedArray[-1].add(NestedInteger(value=int(s[startIndex:idx]))) |
| 109 | + startIndex = None |
| 110 | + elif startIndex == None: |
| 111 | + startIndex = idx |
| 112 | + |
| 113 | + if startIndex != None: |
| 114 | + nestedArray[-1].add(NestedInteger(value=int(s[startIndex:len(s)]))) |
4 | 115 |
|
| 116 | + |
| 117 | + return nestedArray[0].getList()[0] |
5 | 118 | __________________________________________________________________________________________________
|
0 commit comments