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).

Note:

  1. The length of the given string is ≤ 10000.
  2. Each number will contain only one digit.
  3. The conditional expressions group right-to-left (as usual in most languages).
  4. The condition will always be either T or F. That is, the condition will never be a digit.
  5. The result of the expression will always evaluate to either a digit 0-9T or F.

Example 1:

Input: "T?2:3"

Output: "2"

Explanation: If true, then result is 2; otherwise result is 3.

Example 2:

Input: "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 : (T ? 4 : 5))"
-> "(F ? 1 : 4)" or -> "(T ? 4 : 5)"
-> "4" -> "4"

Example 3:

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"
 public class Solution {
public String parseTernary(String expression) {
if (expression == null || expression.length() == )
return "";
Stack<Character> stack = new Stack<>(); for (int i = expression.length() - ; i >= ; i--) {
char c = expression.charAt(i);
if (!stack.isEmpty() && stack.peek() == '?') { stack.pop(); // pop '?'
char first = stack.pop();
stack.pop(); // pop ':'
char second = stack.pop(); if (c == 'T')
stack.push(first);
else
stack.push(second);
} else {
stack.push(c);
}
}
return String.valueOf(stack.peek());
}
}

Ternary Expression Parser的更多相关文章

  1. [LeetCode] Ternary Expression Parser 三元表达式解析器

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  2. Leetcode: Ternary Expression Parser

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  3. LeetCode 439. Ternary Expression Parser

    原题链接在这里:https://leetcode.com/problems/ternary-expression-parser/description/ 题目: Given a string repr ...

  4. PHP Cron Expression Parser ( LARAVEL )

       The PHP cron expression parser can parse a CRON expression, determine if it is due to run, calcul ...

  5. 【Resharper】C# “Simplify conditional ternary expression”

    #事故现场: 对某个对象做空值检测的时候,结合三元运算符给某变量赋值的时候,R#提示:"Simplify conditional ternary expression" : R#建 ...

  6. PythonStudy——三元表达式 Ternary expression

    Python中的三目运算其实就是if...else...的语法糖 # 三目运算符:用于简化 if...else...的语法结构# -- 1) 只能解决if...else...结构,其他if分支结构都不 ...

  7. [LeetCode] Parse Lisp Expression 解析Lisp表达式

    You are given a string expression representing a Lisp-like expression to return the integer value of ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. [LeetCode] Remove Comments 移除注释

    Given a C++ program, remove comments from it. The program source is an array where source[i] is the ...

随机推荐

  1. ajax 多级联动 下拉框 Demo

    写了ajax实现级联下拉框,考虑常用,并且级联个数随不同业务个数不同,于是就整理了一下,实现了 ajax + N级联动 下拉框的效果 效果图 HTML 代码 <h2> 省级联动</h ...

  2. 篇二:MySQL存储过程

    目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似) BEGIN ); ); ) ; ); ); ; ; while count <= insert ...

  3. java-w3c.document生成xml文件

    案例 /** * 创建和写入xml * @param xmlrootname * @param waitConverList */ private void createAndWriterXML(St ...

  4. oracle数据查询

    select * from XMBL_EM_DBBACK where f_djbh='DB01201612270013'select * from flow_task_list where biz_d ...

  5. Java中判断字符串是否为数字的五种方法

    //方法一:用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ ...

  6. C#设备处理类操作

    C#对于处理window操作系统下的设备有天然的优势,对于大多数设备读写等操作来说基本上够了,这里只讨论通过普通的大多数的设备的操作.涉及到两大类SerialPort类,Socket的一些操作.不一定 ...

  7. ubuntu10.04编译安装LAMP

    ubuntu10.04编译安装LAMP以及简单wordpress的使用 : http://linuxme.blog.51cto.com/1850814/971631 一.源码安装LAMP 网上有一堆关 ...

  8. bzoj 4320: ShangHai2006 Homework

    4320: ShangHai2006 Homework Time Limit: 10 Sec Memory Limit: 128 MB Description 1:在人物集合 S 中加入一个新的程序员 ...

  9. IDF-CTF-牛刀小试-啥?

    本人属于Web安全这一块的小白,稍微了作了一下知识补充就开始了CTF,其中的有很多不懂但看多了网上大牛的解题办法和思路.便开始有了一些要想动手记录的冲动,希望大家共同进步学习,本文能对读者有所帮助~ ...

  10. js常用字符串处理方法

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...