Ternary Expression
来源:互联网 发布:6数字域名 编辑:程序博客网 时间:2024/06/07 00:49
Ternary Expression
a?b:c
a
/ \
b c
a?b?c:d:e
a
/ \
b e
/ \
c d
a?b:c?d:e
a
/ \
b c
/ \
d e
input: String expr
output: Expression
expr is valid
format expr: letter mark letter mark letter....
a?b:c
a
/ \
b c
a?b?c:d:e
a
/ \
b e
/ \
c d
a?b:c?d:e
a
/ \
b c
/ \
d e
input: String expr
output: Expression
expr is valid
format expr: letter mark letter mark letter....
public Node ternary(String str) { Stack<Node> stack = new Stack<Node>(); Node root = new Node(str.charAt(0)); Node n = root; for (int i = 1; i < str.length(); i += 2) { if (str.charAt(i) == '?') { n.left = new Node(str.charAt(i + 1)); stack.add(n); n = n.left; } else if (str.charAt(i) == ':') { n = stack.pop(); while(n.right != null) { n = stack.pop(); } n.right = new Node(str.charAt(i + 1)); stack.add(n); n = n.right; } } return root; }
0 0
- Ternary Expression
- 439. Ternary Expression Parser
- 439. Ternary Expression Parser
- leetcode 439. Ternary Expression Parser
- LeetCode 439. Ternary Expression Parser
- Leetcode 439. Ternary Expression Parser (递归,栈)
- [Leetcode] 439. Ternary Expression Parser 解题报告
- Convert a Ternary expression to a Binary tree structure
- Ternary Password
- Ternary Calculation
- Ternary Password
- Ternary Tree
- Ternary Calculation
- [[ expression ]]
- expression
- Expression
- Expression
- Expression
- 2015062009 - 端午祭
- SSH整合
- 卷积神经网络CNN
- fatal error C1189: #error : missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
- 布尔表达式的验证
- Ternary Expression
- 2015062010 - 英雄
- 15HD_OJ——FatMouse's Speed
- CHIP8 Emulator(2)——动手做!
- Struts2获取Web容器资源的方式 访问request,session
- Cygwin中使用ncurses库
- VS高亮显示无扩展名源码文件
- java基础—IO流——将一些字符写入到指定硬盘上的目录中去:
- 输入输出