LeetCode 439. Ternary Expression Parser
原题链接在这里: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:
- The length of the given string is ≤ 10000.
- Each number will contain only one digit.
- The conditional expressions group right-to-left (as usual in most languages).
- The condition will always be either
T
orF
. That is, the condition will never be a digit. - The result of the expression will always evaluate to either a digit
0-9
,T
orF
.
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的更多相关文章
- [LeetCode] Ternary Expression Parser 三元表达式解析器
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- Leetcode: Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- [LeetCode] Parse Lisp Expression 解析Lisp表达式
You are given a string expression representing a Lisp-like expression to return the integer value of ...
- PHP Cron Expression Parser ( LARAVEL )
The PHP cron expression parser can parse a CRON expression, determine if it is due to run, calcul ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 【Resharper】C# “Simplify conditional ternary expression”
#事故现场: 对某个对象做空值检测的时候,结合三元运算符给某变量赋值的时候,R#提示:"Simplify conditional ternary expression" : R#建 ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- LeetCode (10): Regular Expression Matching [HARD]
https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...
随机推荐
- Linux下C++酒店管理系统
功能要求: 相关源码:码云:传送门,GitHub:传送门 相关图片: 拆分版 make编译 ./hotel运行 输入2,进入开房模块 相关源码: class.cpp #include ...
- GO指南练习:切片
最近开始GO语言的学习,在GO指南中练习到切片这个题目 练习:切片 实现 Pic.它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片.当你运行此程序时, ...
- spring cloud各个版本之间的区别
最近公司在使用spring cloud进行开发,对于spring cloud版本号一直有疑惑. 那个版本在前?那个版本在后? 那个版本是最新的? 一.常见版本号说明 举个瓜:2.0.3 RELEASE ...
- Golang_小程序学golang
1 前置条件 Golang基本情况自行baidu/google 1.1 环境与工具 IDE:liteide (windows ).mingw-w64 (gcc) DB:SQL Server 2008 ...
- C#直接调用.mdf文件
一般情况下,.mdf文件都是作为MSSQL的数据库文件,只有在安装了Microsoft SQL Server才能实现调用. 事实上,除此之外,也可以直接调用.mdf文件,而无需安装Microsoft ...
- React: JSX生成真实DOM结点
在上一篇文章中,我们介绍了 Babel 是如何将 JSX 代码编译成可执行代码的,随后也实现了一个自己的解析器,模拟了 Babel 编译的过程. 现在我们再来回顾一下,假定有如下业务代码: const ...
- php后端模式,php-fpm以及php-cgi, fast-cgi,以及与nginx的关系
关于cgi是什么,fast-cgi是什么,php-cgi是什么,fast-cgi是什么,下面这篇讲的很清楚: https://segmentfault.com/q/1010000000256516 另 ...
- Java-最常用的Java日志框架整理
Java-最常用的Java日志框架整理 前言 Java程序员,我们开发了很多Java应用程序,包括桌面应用.WEB应用以及移动应用.然而日志系统是一个成熟Java应用所必不可少的,在开发和调试阶段,日 ...
- 静态Pod
静态Pod kubernetes 除了我们常用的普通Pod外,还有一种特殊的Pod,叫静态Pod. 概念 静态Pod是直接由节点kubelet进程来管理的,不能通过apiserver的master节点 ...
- 51nod 2502 最多分成多少块
小b有个长度为n的数组a,她想将这个数组排序. 然而小b很懒,她觉得对整个数组排序太累了,因此她请你将a分成一些块,使得她只需要对每一块分别排序,就能将整个数组排序. 请问你最多能把a分成多少块. 保 ...