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"的更多相关文章

  1. Expression Tree Build

    The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...

  2. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  3. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  4. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  5. lintcode :Segmemt Tree Build II

    题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...

  6. 【C#表达式树 开篇】 Expression Tree - 动态语言

    .NET 3.5中新增的表达式树(Expression Tree)特性,第一次在.NET平台中引入了"逻辑即数据"的概念.也就是说,我们可以在代码里使用高级语言的形式编写一段逻辑, ...

  7. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  8. Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper

    表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...

  9. 使用Expression Tree构建动态LINQ查询

    这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...

随机推荐

  1. Qt之QFileSystemWatcher

    简述 QFileSystemWatcher类用于提供监视文件和目录修改的接口. QFileSystemWatcher通过监控指定路径的列表,监视文件系统中文件和目录的变更. 调用addPath()函数 ...

  2. 理解 %IOWAIT (%WIO)

    %iowait 是 “sar -u” 等工具检查CPU使用率时显示的一个指标,在Linux上显示为 %iowait,在有的Unix版本上显示为 %wio,含义都是一样的,这个指标常常被误读,很多人把它 ...

  3. ES6—解构赋值

    1.什么是解构赋值 ES6允许按照预定的模式,从数组.对象中提取值,对变量进行赋值. 我们直接用例子说明.    2. 数组的解构赋值 数组传统的变量赋值:      var arr=[1,2,3]; ...

  4. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  5. HDU 2085 核反应堆 --- 简单递推

    HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...

  6. linux 下查看机器是cpu是几核的(转)

    几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpu ...

  7. MySql的FIND_IN_SET()查询函数的使用

    表 table的结构如下: id title 1 12,21 2 21,32 3 45,21,78 4 221,45,74 5 34,421,121 6 52,21,321 现在用sql语句查出来字段 ...

  8. ExtJS控件样式修改及美化

    Extjs项目对富客户端开发提供了强有力的支持,甚至改变了前端的开发方式,使得开发变得更加趋向于“面向组件”.对界面的美化而言,也是根本性的改变.普通的网页美工面对extjs项目根本无法下手,需要脚本 ...

  9. php开发memcached

    一.memcached 简介 memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展 性.它可以应对任意 ...

  10. Lucene 对文档打分的规则整理记录

    摘引自:http://www.cnblogs.com/forfuture1978/archive/2010/02/08/1666137.html Lucene的搜索结果默认按相关度排序,这个相关度排序 ...