LeetCode OJ Basic Calculator II
Basic Calculator II
题目
思路
和这个一样:Basic Calculator I
代码
class ExpressionTransformation {
public:
string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式
long long int calculate_from_postfix_expression(); // 依据后缀表达式计算值
private:
vector<string> ans_vector_post; // 存放后缀表达式
string post_string; // 存放后缀表达式
};
inline int prior(char op) { // 计算优先级函数
if (op == '+' || op == '-') {
return 1;
}
else if (op == '*' || op == '/' || op == '%') {
return 2;
}
else {
return 0;
}
}
long long int string_to_int(string in) { // 将输入的字符串转化为对应数字函数
char s[50];
for (int i = 0; i < 50; i++) {
s[i] = '\0';
}
for (int i = 0; i < in.size(); i++) {
s[i] = in[i];
}
long long int ans;
sscanf(s, "%lld", &ans);
return ans;
}
string deleteBlank(string in) {
string ans;
int size = in.size();
for (int i = 0; i < size; i++) {
if (in[i] != ' ') ans.push_back(in[i]);
}
return ans;
}
string ExpressionTransformation::trans_to_postfix_expression_to_s(string in) {
stack<char> op; // 操作符栈
ans_vector_post.clear(); // 后缀表达式存放数组
for (int i = 0; i < in.size();) {
char c = in[i];
if ('0' <= c && c <= '9') { // 是数字直接插入
string num;
int j;
for (j = i; j < in.size() && '0' <= in[j] && in[j] <= '9'; j++) {
num.push_back(in[j]);
}
ans_vector_post.push_back(num);
i = j;
}
else {
if (c == '(') { // 是开括号直接插入
op.push('(');
}
else {
if (c == ')') { // 是闭括号就把原本栈中的运算符都输出,直到遇到开括号,注意开括号要丢弃
while (op.top() != '(') {
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
}
op.pop();
}
else { // 假如是加减乘除取余
if (op.empty()) { // 操作符栈是空就直接插入
op.push(c);
}
else { // 假设扫描到的运算符优先级高于栈顶运算符则,把运算符压入栈。否则的话,就依次把栈中运算符弹出加到数组ans的末尾,直到遇到优先级低于扫描到的运算符或栈空。而且把扫描到的运算符压入栈中
if (prior(c) > prior(op.top())) {
op.push(c);
}
else {
while (!op.empty() && prior(c) <= prior(op.top())) {
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
}
op.push(c);
}
}
}
}
i++;
}
}
while (!op.empty()) { // 注意把操作符栈中的剩余操作符输出
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
}
post_string.clear(); // 构造string并返回
for (int i = 0; i < ans_vector_post.size(); i++) {
post_string += ans_vector_post[i];
}
return post_string;
}
long long int ExpressionTransformation::calculate_from_postfix_expression() {
//利用栈对后缀表达式求值。直接从后缀表达式的左往右扫描。遇到数字放入栈中,遇到字符就把栈顶的两个数字拿出来算。然后再放进栈
stack<long long int> ans_post;
for (int i = 0; i < ans_vector_post.size(); i++) {
long long int x, y;
if ('0' <= ans_vector_post[i][0] && ans_vector_post[i][0] <= '9') {
ans_post.push(string_to_int(ans_vector_post[i]));
}
else {
y = ans_post.top(); // 注意顺序,这里好比xy+就是x+y
ans_post.pop();
x = ans_post.top();
ans_post.pop();
if (ans_vector_post[i][0] == '+') {
ans_post.push(x + y);
}
else if (ans_vector_post[i][0] == '-') {
ans_post.push(x - y);
}
else if (ans_vector_post[i][0] == '*') {
ans_post.push(x * y);
}
else if (ans_vector_post[i][0] == '/') {
ans_post.push(x / y);
}
else {
ans_post.push(x % y);
}
}
}
return ans_post.top();
}
class Solution {
public:
int calculate(string s) {
ExpressionTransformation e;
s = deleteBlank(s);
e.trans_to_postfix_expression_to_s(s);
int postAns = e.calculate_from_postfix_expression();
return postAns;
}
};LeetCode OJ Basic Calculator II的更多相关文章
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
随机推荐
- python学习---【__all__】
python包中都会有一个__init__.py的模块,这个模块是区分该父文件是一个python包,或是一个文件目录.这个__init__.py可以是空,也可以添加内容,最常见的就是其中的__all_ ...
- caioj 1084 动态规划入门(非常规DP8:任务安排)(取消后效性)
这道题的难点在于,前面分组的时间会影响到后面的结果 也就是有后效性,这样是不能用dp的 所以我们要想办法取消后效性 那么,我们就可以把影响加上去,也就是当前这一组加上了s 那么就把s对后面的影响全部加 ...
- OpenJDK源码研究笔记(七)–Java字节码文件(.class)的结构
最近在看OpenJDK源码的过程中,顺便看了Java编译器(javac)的源码. 为了理解javac的源码,需要先搞懂Java字节码文件(.class)的结构. 于是,我就认真看了下OpenJDK中J ...
- 解决Struts中文乱码问题总结
在进行struts开发的过程中.总也是出现非常多的乱码问题.但归根究竟,也仅仅是下面三种情况: ㈠页面显示中文乱码 ㈡传递參数中文乱码 ㈢国际化资源文件乱码 以下就这三中情况介绍怎么在详细项目 ...
- scikit-learn:3.2. Grid Search: Searching for estimator parameters
參考:http://scikit-learn.org/stable/modules/grid_search.html GridSearchCV通过(蛮力)搜索參数空间(參数的全部可能组合).寻找最好的 ...
- [BZOJ4826][HNOI2017]影魔 可持久化线段树
链接 题意:给你 \(1\) 到 \(n\) 的排列 \(k_1,k_2,\dots,k_n\) ,对 \(i,j (i<j)\)来说,若不存在 \(k_s (i<s<j)\) 大于 ...
- sql 向上取整 向下取整 四舍五入的实例;
SELECT CEILING(23.5/4)'向上取整' ---6 :SELECT FLOOR(23.5/4)'向下取整' ---5 :SELECT ROUND(23.5/4,1)'四舍五入' --5 ...
- jquery基本Dom操作
1 html()获取所有的html内容 2 html(value) 设置html内容,有html自动解析 3 text() 获取文本内容 4 text(value) 设置文本内容,有html自动转义 ...
- UESTC 1599 wtmsb
这天,AutSky_JadeK看到了n张图片,他忍不住说道:“我TM社保!”. 每张图片有一个社保值,他可以合并两张图片,合并所得的图片的社保值是原来两张图片的社保值之和. 每次合并需要消耗的体力也是 ...
- Fedora27 安装Adobe Flash Player PPAPI与NPAPI实现Firefox和Chromium视频播放
一.Adobe Flash Player PPAPI与NPAPI有什么区别我们在打开网页视频时有时会弹出没有安装Flash插件的提示,此时就无法观看视频.Adobe Flash Player是浏览器显 ...