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. USB 描述符

    标准的USB设备有5种USB描述符:设备描述符,配置描述符,字符串描述符,接口描述符,端点描述符. // Standard Device Descriptor typedef struct { u8 ...

  2. 解决load 函数无法赋予变量名的问题

    以前非常喜欢使用load函数,因为简单,而且存储相对较大的matrix.list文件更为方便.但是load函数有一个问题是在使用其过程中无法对其载入的data赋予变量名: # save data x ...

  3. iOS学习笔记---C语言第三天

    循环结构 : while循环   do...while循环(几乎不用)     for循环(使用最多) 特点:在给定的条件成立时,反复执行某程序段,直到条件不成立为止. 给定的条件为循环条件,反复执行 ...

  4. dede模版列表调用文章正文内容的方法

    在制作织梦模板的时候,有的时候我们需要调用文章部分内容,用[field:description/]标签字数不够多(数据库设计字段是varchar(255)的),另外修改了文章内容但是摘要还需要手动修改 ...

  5. Indexof

    注释:indexOf() 方法对大小写敏感! 注释:如果要检索的字符串值没有出现,则该方法返回 -1. 实例 在本例中,我们将在 "Hello world!" 字符串内进行不同的检 ...

  6. ZOJ 1005 Jugs

    原题链接 题目大意:有一大一小两个杯子,相互倒水,直到其中一个杯子里剩下特定体积的水.描述这个过程. 解法:因为两个杯子的容积互质,所以只要用小杯子不断往大杯子倒水,大杯子灌满后就清空,大杯子里迟早会 ...

  7. Java字段初始化的规律

    class InitializeBookClass { { field=200; } public int field=100; public InitializeBookClass(int valu ...

  8. webStorm快捷键总结

    Ctrl+Shift+a:快速查找使用编辑器所有功能1.左侧栏目录显影:Ctrl+Shift+F122.文件模板配置:File>Settings>Editor>File and Co ...

  9. mysql 关联删除

    参考网址:http://www.111cn.net/database/mysql/51146.htm 原网页广告太多,自己抄了下. 1.delete from t1 where 条件2.delete ...

  10. root密码

    安装完Ubuntu后忽然意识到没有设 置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的 root密码.我 ...