数据结构与算法(1)支线任务2——Basic Calculator
题目:
https://leetcode.com/problems/basic-calculator/
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
思路一:
按照运算符优先级将原表达式转化为波兰表达式后缀式,然后再计算后缀式,与之前作业方法一致。
然而“前人”的博客已经写得很好了(见文末链接),上节课又刚好讲了递归,于是有了另一个作死的思路。
思路二:
计算只有加减的表达式,遇到括号递归调用该函数。关键部分如下:
for (i = ; i < ori.size() - c; i++)
{
a = ori[i+c];
switch (a)
{
//为简洁删掉其余case和default部分的代码
case '(':
str.assign(ori, i+c+, ori.size()-i-c-);
c += getOp(str, m)+;
elem.e.m = m;
elem.isChar = ;
op.push_back(elem);
break;
case ')':
while (!s.empty())
{
t = s.top();
s.pop();
elem.e.c = t;
elem.isChar = ;
op.push_back(elem);
}
n = calOp(op);
return i;
break;
}
}
然而在括号过多时,算法复杂度为O(n2),遇到文章最后的给出的输入会超时(此处不想吐槽……)
思路三:
对该题目,其实只需要判断每个数应该进行的操作('+'/'-'),于是可以只用一个用于记录当前(括号外)的加或减的栈,再对括号内每个数判断其前面的符号进行相应操作。这样只需遍历一次,复杂度O(n)。不过还要注意空格的问题。代码如下:
int calculate(string s) {
stack<bool> isPos;//当前括号外应执行的操作,'+'为1
char c;
bool curPos = ;//当前待执行的操作,'+'为1
isPos.push();//初始操作为'+'
int sum = , t;
for (int i = ; i < s.size(); i++)
{
c = s[i];
switch (c)
{
//判断当前应执行的操作
case '+':
if (isPos.top() == )
{
curPos = ;
}
else
{
curPos = ;
}
break;
case '-':
if (isPos.top() == )
{
curPos = ;
}
else
{
curPos = ;
}
break;
//遇到左括号更新括号外的操作
case '(':
isPos.push(curPos);
break;
//遇到右括号弹出当前括号外的操作,恢复为上一个括号内
case ')':
isPos.pop();
break;
//遇到数字继续扫描至符号
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
t = ;
while (s[i] >= '' && s[i] <= '')
{
t = *t + s[i] - '';
i++;
}
i--;
if (curPos == )
{
sum -= t;
}
else
{
sum += t;
}
break;
//忽略其他的符号(其实只有空格)
default:
break;
}
}
return sum;
}
”前人“们的博客:
http://200404.sinaapp.com/2015/10/evaluating-an-arithmetic-expression/ http://www.cnblogs.com/lustralisk/p/brance-2.htm http://jmq14.github.io/2015/10/23/calculator/思路二没通过的例子:
"(9-(10-(10-0-(3+(8+(0+(8-(10-8-(7-(2+(5+(6+(10+(3+(8+(3-(9+(1+(10+(1-(1+(6-2+0+(10-(9-(3-(3-9-(1-(7+(4-(2+(2-(10+(3+(7-(1-(4+(1+(1-(10-(5-(9+(9-4-(5-(1+8-(2-(1+(1-10-(4-(1+(4-(7)-(3-(8)+(5+5-(5-(9-(8+(8-4-1+(0-(1+(1+(10-(7+(2-(5-(4-(6+(2+(1-(2-(9+8+(2+(9-(9-(7+(10+1+(5)))-(2-(8+3+(5-(7-(3+(9)+(10+(0+(8-(1-(9)-(0+10-(3+(9-(0-(5-(7-(4-4+1+(7)-(10+(5+(9-(3+(5+(6-(0-(7-(1-(4+(6+(4-2-(4+(9-(6+9-8+1+(5+(7-9+3)+(10-(10+(2+(0-(5-(2+(10-(4-5-(7-(4-(7+(4)+6+10+(2-(7+(2))+(1)+(5-(7)-(10-(5+(7-(6-(2+(1-4)+(10-(5)+(4+(10+(4+(0+(10+(8-(8+(6+5-(1-(6-(1-(2+(4+(9-(3+(1+(10+(4)+(0+(3-(2-(9-(2-(3-(4-(2+(7-(6-(5+(7+(5+(5-(4+(0-(7+(2-(7+(9)-(6-(10)+(7+(2-(9-(9)+(4+(1-(8+(2-0-(2+(2+10)-(7-9-(9+(8-(5-8-(5)+(6+(10-(3-(2-(2+(7-2+(9+(3+(9+(2-(8+(5-(4+(4-(1-(9+(0+(6-(4-(3+(5-(2-(4-(6+(0+(4+3)-(8-(6+(9+(1+(2)-(8-(1+1+(5+(4-(3-(1-(7-4+(6+(9+(1+(4)+(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))-(1+(2+(10))))+(6)+(0+(9-(1)-(10)))+(10-(1-(1)))-(0+(0-(2-(4-(6+(1))+(0)+(5)-(5+(5)-(4+(6)-(5)+(1-(7))))+(8)-(7))-3))-(7+(7+(9+(0+(10)-(7-(0-(2)-(6))-(2+(10)))+(7)+(3))+(8-(8+(10)-(8)+(0+(6-(2)-(1))+(3+(10+(10-(4+(7-(2)-(9-(2+(8))))+(7)-(7+10+(9-(2)+(0))-(6+(1)))+(10)+(2)-(7)-(4)-(10+(3-(6))))+(8-(1))-(10)))+(5+(3-(0-(1-(2+(3-(6-(4)-(1)+(4+(7+(3)-(7)+(4-(9))+(0-(4)+(9+(3-(9)+(4-(10+(6+(4)))+(4))+(10+(0-3-(8+(0-(6))-(5))-(9))-(6))))+2))+6+(6)+(1-(6))-(7-(1))-(8)+(9-(8))+(4)))-(0+7-(1)))-(2))+(0)))+(4-(7))-(5)-(8)-4+(1-(3-(8+(2+0)+(7)))))))-(4-(2))+(9))))+(7)-(2-(10+(4)-(8+(7)+(5-(4)-(6+6))-(2+(6)-(2+(4-(2-(8-(4)-(7+(5)-(10-(7)))))-(10+(9+(8)-(10)+(3-(7+(4+(2+(5)-(10+7+(2-(10)-(10+(3))+(0-(10+(8+(4+(7-(2)+(3+9))))+(7-(6+(2)-(2)+7+(5+(7+(10+(5-(4)-2+(5)+(1))+(0))))-(9))-5-(8)-(9-(4)-(10))-(8-(5)-(10)-7)+(5))-(4)))))+6+3+(3+(6+(9)))-10+(6)+(0)))))+(7)))+(1-(5)+(3-(3+6))+(5)+(7)-(9-(1))+(4+(1))+(2)))-(3))-(10)+(1)))))))))+(3)+2+(8-(4)))-(1))+(6-(8-(0)-(8-(0))-(2-(4+2)))-(9+1)))-(8-(8+(1-8-(7))))+7-(5+(5+(6+(10)+(8)))))))-(4))))-(4)-(6)+(10)-(5)))+(0+(2+(4))-(4-(2)+(0-(10-(4))))))+3-(10)))-(9+(9-(8-(7)-4))))+(6))-(4-(9))))-(1))+(10))))-(0+(9+7-(1)))))-(7)-(4)))-(9))))-7))))+(9))+(10))-(8-(9)))+(8))-(6)-(4)-(8)))))))))))))-(7)))))+(2-(6)-(0))))-(0)-(5+(9)+(9))+(3-(9))))+(8))))))))-(0-(0))+(7-(2))))))))-(6))-(8+(9))-(9+(2))-(2)+(9))-(4))+(7)-(1)-(6))-(2-0)))))))-(0)))))-(8+(0-(5))))+(9)-(1-(0)-(3)))-(3)-(0)))+(4)+(6))))-(5)+(1-(5)))))+(10))))-(5)+(0))))))-(6)))))))+(1))))))))-(5)))))))))+(8))))))))))))))))))-(7)+(10)))))))))))))-(4))))))-(10)-(4))+(1)+(3))-(1))))+(9))))))))+(2-(7-(4-(3+(0))))-(10)))))+0))))+(10)))))+(4)))))))))))+(3)))))))-(5)))))+(3)))))))))))))-(7)-(5-(2+(9))-(0))+(4)))+(10)))))-(1)))-(0))+(1))-(8+(10))))))-(10)-(10+(9)+(2))))-(1)))-(2))))+(4+(5))))))+(8))))))))))))))))))))))-(7)))-(3)))))))+(1))))-(7)-(3)+(4))))))-(6)))))))-(9-(3)))))))))))+(8))))))))))+(6))))))))))))))))))))))))))))))))))+(5))+(7))))))))))))))))))))))))-(10))))))+9)))))))"
数据结构与算法(1)支线任务2——Basic Calculator的更多相关文章
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- 数据结构与算法(1)支线任务3——Largest Rectangle in Histogram
题目如下:(https://leetcode.com/problems/largest-rectangle-in-histogram/) Given n non-negative integers r ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- 227. Basic Calculator II 无括号版本计算器
[抄题]: Implement a basic calculator to evaluate a simple expression string. The expression string con ...
- Basic Calculator
本博客介绍leetcode上的一道不难,但是比较经典的算法题. 题目如下: Implement a basic calculator to evaluate a simple expression s ...
- 用python语言讲解数据结构与算法
写在前面的话:关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem ...
- [0x00 用Python讲解数据结构与算法] 概览
自从工作后就没什么时间更新博客了,最近抽空学了点Python,觉得Python真的是很强大呀.想来在大学中没有学好数据结构和算法,自己的意志力一直不够坚定,这次想好好看一本书,认真把基本的数据结构和算 ...
- 227. Basic Calculator
1. 问题描述 Implement a basic calculator to evaluate a simple expression string. The expression string c ...
- 数据结构与算法(C/C++版)【绪论/线性表】
声明:数据结构与算法系列博文参考了<天勤高分笔记>.<王道复习指导>.C语言中文网.非商业用途,仅为学习笔记总结! 第一章<绪论> 一.基本概念及入门常识 /// ...
随机推荐
- (原创)VM中的CentOS6.4中安装CloudStack6.3②
接着VM中的CentOS6.4中安装CloudStack6.3①中文章接着,往下面安装 4.更新 yum 仓库 默认情况下,CentOS的软件源中没有收录最新版本CloudStack,为了能顺利安装, ...
- 写一些封装part1 (事件绑定移除,圆形矩形碰撞检测)
var EventHandle = { addEvent:function(ele,type,handle){ if (ele.addEventListener) { ele.addEventList ...
- 让xterm更舒服的设置
转自:http://codespider.is-programmer.com/posts/25247.html X11相关的配置位于/etc/X11下. xterm的配置是/etc/X11/app-d ...
- UVa 221城市正视图(离散化)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- window.onload用法详解:
网页中的javaScript脚本代码往往需要在文档加载完成后才能够去执行,否则可能导致无法获取对象的情况,为了避免这种情况的发生,可以使用以下两种方式: 一.将脚本代码放在网页的底端,这样在运行脚本代 ...
- 找回MSI安装包Win7/Win8管理员身份功能
找回MSI安装包Win7/Win8管理员身份功能 从Vista开始,系统引入了UAC用户控制功能,即普通用户运行exe软件安装程序,支持使用普通账户/管理员身份分别进行安装,但是msi安装包只支持默认 ...
- 【阿迪达斯 ZX850 系列】
[阿迪达斯zx850系列 灰蓝桔 36-44] [阿迪达斯 ZX850 新配色B34720 情侣鞋 36-44] [阿迪达斯 ZX850 新配色B34720 情侣鞋 36-44] [阿迪达斯 ZX85 ...
- (转) WTF is computer vision?
WTF is computer vision? Posted Nov 13, 2016 by Devin Coldewey, Contributor Next Story Someon ...
- MYSQL常见语句
SHOW INDEXES from tablename EXPLAIN tablename EXPLAIN SELECT * FROM tablename
- unity, 按类型查找文件