leetcode 手写计算器 方法总结
1.leetcode 227 Basic calculator 我目前见过的最好的解法
Implement a basic calculator to evaluate a
simple expression string.The expression string contains only non-negative
integers, +, -, *, / operators and empty spaces .
The integer division should truncate toward zero.Example 1:Input: "3+2*2"
Output: 7
Example 2:Input: " 3/2 "
Output: 1
Example 3:Input: " 3+5 / 2 "
Output: 5
AC code
pop 代表上一个是+还是-
pv 代表上一个+ 或者 - 后面的数字是多少
class Solution {
public:int calculate(string s) {istringstream in("+"+ s);long long num = 0;long long cv = 0;int pv = 0;char cop = '#';char pop = '#';while(in >> cop){in >> num;if (cop == '+'){cv += num;pv = num;pop = cop;}else if (cop == '-'){cv -= num;pv = num;pop = cop;}else if (cop == '*'){if (pop == '+') cv = cv - pv + pv*num;else if (pop == '-') cv = cv + pv - pv*num;pv = pv * num;}else if (cop == '/'){if (pop == '+') cv = cv - pv + pv/num;else if (pop == '-') cv = cv + pv - pv/num;pv = pv/num;}}return cv;}
};
2.带括号的四则运算
op != ')'
先判空 再判( 再判数 后判op
#include <vector>
#include <iostream>
#include <string>
using namespace std;int parseNum(string s, int& i)
{long long num = 0;while (i < s.size() && s[i] >= '0' && s[i] <= '9') num = 10 * num + s[i++] - '0';return num;
}int parseExpr(string s, int& i)
{char op = '+';long long n = 0;vector<int> nums;long long res = 0;for (; i < s.size() && op != ')'; i++){char c = s[i];if (c == ' ') continue;else if (c == '(') n = parseExpr(s, ++i);else if (c >= '0' && c <= '9') n = parseNum(s, i);switch (op){case '+':nums.push_back(n);break;case '-':nums.push_back(-n);break;case '*':nums.back() *= n;break;case '/':nums.back() /= n;break;}op = s[i];}for (int num : nums) res += num;return res;
}int calculate(string s)
{int i = 0;return parseExpr(s, i);
}int main() {string s;while (getline(cin , s)){ cout << calculate(s) << endl;}return 0;
}
leetcode 手写计算器 方法总结相关推荐
- 如果数组最后一项为空删除最后一项和去前面的逗号_手写数组方法
最近把数组的常用方法都手写了一遍,整理到了一块儿,顺便就分享出来. 注:有的方法的实现算法可能不是最好的,所以也可以根据自己的理解有不一样的实现方法,欢迎在评论区留出来. 我废话就不多说了,直接上代码 ...
- 多人在线文档(excel)手写签名方法
简介 无论是高校.政府.企业及其他个人团体,都会产生多人对同一文件签名的需求,本文章提供了一种解决方案 软件 钉钉 方法 负责人注册钉钉 创建团队 配置好对应的群设置 将文件传至群内(可开启保密模式, ...
- C语言手写栈、手写计算器
C语言实现表达式求值 文章结构 C语言手写栈Struct Stack 后缀表达式求值 中缀表达式转后缀表达式 文章结构 本文尝试通过纯C语言写一个计算器,在Visual Studio和VScode运行 ...
- 使用inetaddress测试目标可达性_纯java手写打造方法级白盒测试框架
我们在做服务端测试时,经常使用的自动化测试框架或平台大多通过restful风格使用http协议接入系统,例如常见的Jmeter.LoadRunner.Postman等,还有不常用或付费的工具如Test ...
- 手写计算器java_可编程科学计算器app
可编程科学计算器app是一款功能超级强大的科学计算器,它通过自建程序解决很多工程测算的问题!欢迎有需要的朋友在芒果下载站下载体验! 可编程科学计算器app官方介绍 程序开发科学计算器是一个和Matla ...
- 手写 instanceof 方法
语法 object instanceof constructor 参数 object 某个实例对象 constructor 某个构造函数 描述 instanceof 运算符用来检测 construct ...
- java手写计算器,GUI与鼠标适配器的配合
源代码如下:import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; i ...
- JS手写数组扁平化(flat)方法
方法介绍 数组扁平化方法 Array.prototype.flat() 也叫数组拍平.数组拉平.数组降维. Array.prototype.flat() 用于将嵌套的数组"拉平", ...
- 手写字符串 trim 方法,保证浏览器的兼容性
手写字符串 trim 方法,保证浏览器的兼容性 trim() 方法删除字符串首尾空白字符 手写trim方法: String.prototype.trim = function(){return thi ...
最新文章
- 女程序员做编程也太难了吧! | 每日趣闻
- C#开发ActiveX网页截图控件
- 关于Delphi中DLL,BPL等无法调试的问题
- leetcode-无重复字符的最长子串
- Github使用初体验2018.08.07
- [转]自用类库整理之SqlHelper和MySqlHelper
- Linux下C语言执行过程(预处理,编译,汇编,链接,执行)
- java-构建jar带哟参数提示的
- 比较牛逼的答题卡扫描算法
- Root cause be too many concurrent connections error could not open clinet transport with jdbc uri
- LA 3027 并查集
- python快乐数,快乐数 - SegmentFault 思否
- 程序员使用C#编写表白小软件(VS2013)(表白程序)
- mpa和pis_有关压力单位pis-bar-mpa的换算
- 网站服务器后缀名,服务器域名,域名后缀
- mac怎么给移动硬盘分区
- 微信公众平台的发展历史
- sd卡驱动分析 linux,linux下SD卡驅動分析(一)
- itextword加公章 java_新手如何使用itext7生成pdf盖章
- 在互联网时代一切归零