原题链接在这里:https://leetcode.com/problems/ternary-expression-parser/description/

题目:

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"

题解:

从后往前扫输入的string. 把char 放入stack中. 当扫过了'?', 就知道需要开始判断了.

从stack中弹出两个数,判断后的数放回stack中.

Time Complexity: O(n). n = expression.length();

Space: O(n).

AC Java:

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

Track the first string and second string separated by the corresponding " : ".

Based on the true or false, continue DFS on the corresponding string.

Time Complexity: O(n^2). n = expression.length().

Space: O(n).

AC Java:

 class Solution {
public String parseTernary(String expression) {
if(expression.length() == 1){
return expression;
} int indexQuestion = expression.indexOf("?");
boolean flag = expression.charAt(0) == 'T' ? true : false; int count = 0;
int start = indexQuestion+1;
while(expression.charAt(start) != ':' || count != 0){
if(expression.charAt(start) == '?'){
count++;
}else if(expression.charAt(start) == ':'){
count--;
} start++;
} String first = expression.substring(indexQuestion+1, start);
String second = expression.substring(start+1);
return parseTernary(flag ? first : second);
}
}

LeetCode 439. 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. Ternary Expression Parser

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

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

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

  5. PHP Cron Expression Parser ( LARAVEL )

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

  6. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

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

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

  8. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  9. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

随机推荐

  1. Ramdisk根文件系统映像的修改与创建

    本文简述Ramdisk根文件系统映像的修改以及创建,并附相关脚本以实现自动化配置,而根文件系统的制作过程请网上自行搜索.相关过程尽可能以图的方式展示出来,重在说明操作的过程,仅供参考. Ramdisk ...

  2. JZOJ5833 永恒

    题目大意 给你一个树,每个节点上有有一个部落,以及部落的人数,要你求出每个节点的子树里面人数最多的部落是哪一个(人数相同部落编号最小的). 思路 全网第一篇分治题解 考虑树的dfs序,然后分治处理,每 ...

  3. 常用正则表达式和一些demo

    一.校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ ...

  4. 【开发工具】- Myeclipse10.7破解方法

    1.下载myeclipse 10,如果没有,可以使用链接:https://pan.baidu.com/s/1l9juqD4ALMuepVL6e5kgjA 密码:kpx6:当然时间久了可能链接失效,如有 ...

  5. Alpha_4

    一. 站立式会议照片 二. 工作进展 (1) 昨天已完成的工作 a. 我的·主界面设计 b. 番茄钟的页面及音乐选择弹窗页面设计 c. 实现自定义习惯和设置新习惯的功能页面,并可预览 d.已实现番茄钟 ...

  6. 深入理解JVM-java虚拟机栈

    1.java虚拟机栈 1. Java虚拟机栈也是线程私有的,它的生命周期与线程相同(随线程而生,随线程而灭) 2. 如果线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowErro ...

  7. oracle执行计划(一)----概述

    (1)什么是执行计划SQL是一种傻瓜式语言,每一个条件就是一个需求,访问的顺序不同就形成了不同的执行计划.Oracle必须做出选择,一次只能有一种访问路径.一个访问路径就是一个执行计划. (2)执行计 ...

  8. springboot注解@NotNull,@NotBlank,@Valid自动判定空值

    一.前言 搭建springboot项目,我们都是采用的Restful接口,那么问题来了,当前端调用接口或者是其他项目调用时,我们不能单一靠调用方来控制参数的准确性,自己也要对一些非空的值进行判定. 二 ...

  9. 乌班图安装Lnmp环境

    1.nginx //切到root用户 sudo su //更新源 apt-get update //安装 apt-get install nginx //安装完成后配置文件目录 cd /etc/ngi ...

  10. nginx的stream模块和upstream模块

    nginx7层调度方式 使用upstream模块定义集群名称和节点地址 定义在server字段之外httpd字段之内 upstream staticweb { server 172.17.0.2; # ...