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
TorF. That is, the condition will never be a digit. - The result of the expression will always evaluate to either a digit
0-9,TorF.
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 ...
随机推荐
- docker关系图解析
docker关系图解析 一.docker有5种状态 Dockerfile 文本文件,制作images的配置文件 images image,静态文件 containers container image ...
- mysql 复制表结构(包括索引等)、表内容
=============================================== 2019/7/16_第1次修改 ccb_warlock == ...
- [SDOI2008]仪仗队(欧拉函数)
题目 [SDOI2008]仪仗队 解析 这个题,我也不知道他们的soltion是怎么写的这么长的. 我们发现我们一次看一条直线上的第一个点,也就是说,若两个点斜率\(k=\frac{y}{x}\)相同 ...
- 【转载】C#中使用float.TryParse方法将字符串转换为Float类型
在C#编程过程中,将字符串string转换为单精度float类型过程中,时常使用float.Parse方法,但float.Parse在无法转换的时候,会抛出程序异常,其实还有个float.TryPar ...
- 彻底弄懂ES6中的Map和Set
Map Map对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值.构造函数Map可以接受一个数组作为参数. Map和Object的区别 一个Object 的键只能是字符串或者 Symb ...
- 提取线条的lines_color、lines_facet、 lines_gauss算子
Halcon中线条提取的算子主要有: lines_color(Image : Lines : Sigma, Low, High, ExtractWidth, CompleteJunctions : ) ...
- Xcode调试打印方法
1 NSLog 在调试的过程中,最常用的查看变量值的方法是NSLog 整数 int a = 1; NSLog("%d", a); 浮点数 float b = 1.11; NSLog ...
- Java 缓存实例
重复创建相同的对象没有太大的意义,反而加大了系统开销,某些情况下,可以缓存该类的实例,实现复用. 实现缓存实例:定义一个private static成员变量存储类的实例(多个可用数组)先检测上面的成员 ...
- k8s node节点部署(v1.13.10)
系统环境: node节点 操作系统: CentOS-7-x86_64-DVD-1908.iso node节点 IP地址: 192.168.1.204 node节点 hostname(主机名, 请和保持 ...
- CentOS 7网络配置工具
CentOS 7网络配置工具 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.网卡命名机制 CentOS 6之前,网络接口使用连续号码命名:eth0.eth1等,当增加或删除网卡 ...