返回介绍

solution / 0400-0499 / 0439.Ternary Expression Parser / README_EN

发布于 2024-06-17 01:04:00 字数 5351 浏览 0 评论 0 收藏 0

439. Ternary Expression Parser

中文文档

Description

Given a string expression representing arbitrarily nested ternary expressions, evaluate the expression, and return _the result of it_.

You can always assume that the given expression is valid and only contains digits, '?', ':', 'T', and 'F' where 'T' is true and 'F' is false. All the numbers in the expression are one-digit numbers (i.e., in the range [0, 9]).

The conditional expressions group right-to-left (as usual in most languages), and the result of the expression will always evaluate to either a digit, 'T' or 'F'.

 

Example 1:

Input: expression = "T?2:3"
Output: "2"
Explanation: If true, then result is 2; otherwise result is 3.

Example 2:

Input: expression = "F?1:T?4:5"
Output: "4"
Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:
"(F ? 1 : (T ? 4 : 5))" --> "(F ? 1 : 4)" --> "4"
or "(F ? 1 : (T ? 4 : 5))" --> "(T ? 4 : 5)" --> "4"

Example 3:

Input: expression = "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 ? F : 3)" --> "F"
"(T ? (T ? F : 5) : 3)" --> "(T ? F : 5)" --> "F"

 

Constraints:

  • 5 <= expression.length <= 104
  • expression consists of digits, 'T', 'F', '?', and ':'.
  • It is guaranteed that expression is a valid ternary expression and that each number is a one-digit number.

Solutions

Solution 1

class Solution:
  def parseTernary(self, expression: str) -> str:
    stk = []
    cond = False
    for c in expression[::-1]:
      if c == ':':
        continue
      if c == '?':
        cond = True
      else:
        if cond:
          if c == 'T':
            x = stk.pop()
            stk.pop()
            stk.append(x)
          else:
            stk.pop()
          cond = False
        else:
          stk.append(c)
    return stk[0]
class Solution {
  public String parseTernary(String expression) {
    Deque<Character> stk = new ArrayDeque<>();
    boolean cond = false;
    for (int i = expression.length() - 1; i >= 0; --i) {
      char c = expression.charAt(i);
      if (c == ':') {
        continue;
      }
      if (c == '?') {
        cond = true;
      } else {
        if (cond) {
          if (c == 'T') {
            char x = stk.pop();
            stk.pop();
            stk.push(x);
          } else {
            stk.pop();
          }
          cond = false;
        } else {
          stk.push(c);
        }
      }
    }
    return String.valueOf(stk.peek());
  }
}
class Solution {
public:
  string parseTernary(string expression) {
    string stk;
    bool cond = false;
    reverse(expression.begin(), expression.end());
    for (char& c : expression) {
      if (c == ':') {
        continue;
      }
      if (c == '?') {
        cond = true;
      } else {
        if (cond) {
          if (c == 'T') {
            char x = stk.back();
            stk.pop_back();
            stk.pop_back();
            stk.push_back(x);
          } else {
            stk.pop_back();
          }
          cond = false;
        } else {
          stk.push_back(c);
        }
      }
    }
    return {stk[0]};
  }
};
func parseTernary(expression string) string {
  stk := []byte{}
  cond := false
  for i := len(expression) - 1; i >= 0; i-- {
    c := expression[i]
    if c == ':' {
      continue
    }
    if c == '?' {
      cond = true
    } else {
      if cond {
        if c == 'T' {
          x := stk[len(stk)-1]
          stk = stk[:len(stk)-2]
          stk = append(stk, x)
        } else {
          stk = stk[:len(stk)-1]
        }
        cond = false
      } else {
        stk = append(stk, c)
      }
    }
  }
  return string(stk[0])
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文