385 迷你語法分析器

2021-09-29 10:08:41 字數 1373 閱讀 6983

給定乙個用字串表示的整數的巢狀列表,實現乙個解析它的語法分析器。

列表中的每個元素只可能是整數或整數巢狀列表

字串非空

字串不包含空格

字串只包含數字0-9

,[,-

,,]

示例 1:

給定 s =

"324"

,你應該返回乙個 nestedinteger 物件,其中只包含整數值 324。

示例 2:

給定 s =

"[123,[456,[789]]]"

,返回乙個 nestedinteger 物件包含乙個有兩個元素的巢狀列表:

1. 乙個 integer 包含值 123

2. 乙個包含兩個元素的巢狀列表:

i. 乙個 integer 包含值 456

ii. 乙個包含乙個元素的巢狀列表

a. 乙個 integer 包含值 789

/**

* // this is the inte***ce that allows for creating nested lists.

* // you should not implement it, or speculate about its implementation

* public inte***ce nestedinteger

*/class

solution

stack

stack =

newstack

<

>()

; stack

numstack =

newstack

<

>()

; stringbuilder b =

newstringbuilder()

;for

(char c : s.

tochararray()

)else

if(c ==

']')

list

list=

newarraylist

<

>()

;while

(true)if

(z==

'[')}if

(list.

size()

>0)

stack.

push

('.');

numstack.

push

(n);

}else

}else

}else}}

return numstack.

pop();

}}

385 迷你語法分析器

題目描述 示例 1 給定 s 324 你應該返回乙個 nestedinteger 物件,其中只包含整數值 324。示例 2 給定 s 123,456,789 返回乙個 nestedinteger 物件包含乙個有兩個元素的巢狀列表 乙個 integer 包含值 123 乙個包含兩個元素的巢狀列表 i....

LeetCode 迷你語法分析器

給定乙個用字串表示的整數的巢狀列表,實現乙個解析它的語法分析器。列表中的每個元素只可能是整數或整數巢狀列表 你可以假定這些字串都是格式良好的 字串非空 字串不包含空格 字串只包含數字0 9,示例 1 給定 s 324 你應該返回乙個 nestedinteger 物件,其中只包含整數值 324。示例 ...

LR 語法分析器

lr語法分析器算是基本完成了,只需要乙個文法定義檔案 syntax 就可以進行對應語言的語法分析,最後形成語法樹。詞法分析是固定的,採用c 的詞法定義。以後將加入動態的詞法分析。壓縮包中檔案的描述 lrtable.exe 是用文法定義檔案 syntax檔案 生成lr動作表檔案 action檔案 使用...