LintCode "Expression Tree Build"
Lesson learnt: for any calculator problems, keep 2 stacks: 1 for operators and 1 for operands.
class Solution
{
stack<ExpressionTreeNode*> op;
stack<ExpressionTreeNode*> data; void eval()
{
ExpressionTreeNode *opnode = op.top(); op.pop();
ExpressionTreeNode *data1 = data.top(); data.pop();
ExpressionTreeNode *data2 = data.top(); data.pop();
opnode->left = data2;
opnode->right = data1;
data.push(opnode);
}
public:
ExpressionTreeNode* build(vector<string> &expression)
{
for(auto &tmp : expression)
{
ExpressionTreeNode *node = new ExpressionTreeNode(tmp); switch(tmp[])
{
case '(':
op.push(node);
break;
case '+':
case '-':
while(!op.empty()&&op.top()->symbol[]!='(')
{
eval();
}
op.push(node);
break;
case '*':
case '/':
while(!op.empty()&&(op.top()->symbol[]=='*'||op.top()->symbol[]=='/'))
{
eval();
}
op.push(node);
break;
case ')':
while(op.top()->symbol[]!='(')
{
eval();
}
op.pop();
break;
default:
data.push(node);
break;
}
}
while(!op.empty())
{
eval();
} if(data.empty()) return nullptr;
return data.top();
}
};
LintCode "Expression Tree Build"的更多相关文章
- Expression Tree Build
The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
- 【C#表达式树 开篇】 Expression Tree - 动态语言
.NET 3.5中新增的表达式树(Expression Tree)特性,第一次在.NET平台中引入了"逻辑即数据"的概念.也就是说,我们可以在代码里使用高级语言的形式编写一段逻辑, ...
- Expression Tree Basics 表达式树原理
variable point to code variable expression tree data structure lamda expression anonymous function 原 ...
- Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper
表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...
- 使用Expression Tree构建动态LINQ查询
这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...
随机推荐
- OGRE 2.1 Windows 编译
版权所有,转载请注明链接 OGRE 2.1 Windows 编译 环境: Windows 7 64Bit Visual Studio 2012 OGRE 2.1 CMake 2.8.12.1 OGRE ...
- Ubuntu系统的修改Hosts
1.修改hostssudo gedit /etc/hosts2.添加解析记录( . )完整案例:127.0.0.1 localhost.localdomain localhost简洁记录:127.0. ...
- Linux(Ubuntu 13)下安装Eclipse
1.本机配置及软件情况: Ubuntu版本:Ubuntukylin-13.10-desktop-i386(32位) JDK:jdk1.7.0_15(jdk-7u15-i856.gz) eclipse: ...
- CentOS7上安装和使用Docker
导读 Docker 是一个开源工具,它可以让创建和管理 Linux 容器变得简单,容器就像是轻量级的虚拟机,并且可以以毫秒级的速度来启动或停止.在本篇文章中我们将教你如何在 CentOS 7.x 中安 ...
- sgu548 Dragons and Princesses 贪心+优先队列
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...
- HTTP详解(2)
HTTP详解(2)-请求.响应.缓存 分类: 网络知识2013-03-17 16:45 1969人阅读 评论(0) 收藏 举报 目录(?)[+] 1. HTTP请求格式 做过Socket编程的 ...
- puppet安装配置及使用
puppet安装前准备 一.服务器信息 master端:10.10.10.201 master.fansik.com slave端:10.10.10.156 slave.fansik.com 三台机 ...
- URAL 1076 Trash Trash(最大权匹配)
Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...
- URAL 2034 Caravans(变态最短路)
Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...
- c语言学习笔记
为什么需要输出控制符: 1: 01组成的代码可以表示数据亦可以表示指令: 2:如果01组成的代码表示的是数据的话,那么同样的01代码组合以不同的输出格式输出就会有不同的输出结果.. %d --- ...