LC439. Ternary Expression Parser - freeCookie🍪 Blog
LC439. Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. You can always assume that the given expression is valid and only consists of digits 0-9
, ?
, :
, T
and F
(T
and F
represent True and False respectively).
Example:
Input: "T?T?F:5:3" Output: "F" Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as: "(T ? (T ? F : 5) : 3)" "(T ? (T ? F : 5) : 3)" -> "(T ? F : 3)" or -> "(T ? F : 5)" -> "F" -> "F"
实现?:这个表达式,记录?和:的位置,利用count找到最外层的表达式,利用recursion一层一层操作。
Read full article from LC439. Ternary Expression Parser - freeCookie🍪 Blog
No comments:
Post a Comment