LeetCode224——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.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in
library function.
实现:
void skipws(string &s, int * beg, bool b = false)
{
if (b) {
while (s[*beg] == ' ') {
(*beg)++;
}
} else {
while (s[*beg] == ' ' || s[*beg] == '(' || s[*beg] == ')') {
(*beg)++;
}
}
}
int numend(string &s, int beg)
{
while (s[beg] >= '0' && s[beg] <= '9') {
beg++;
}
return beg;
}
int parenthesisend(string& s, int beg){
int brace = 0;
while (s[beg] != ')' || brace != 0) {
if (s[beg] == '(') {
brace++;
} else if (s[beg] == ')') {
brace--;
}
beg++;
}
return beg;
}
int calculate(string s) {
int start = 0;
int result = 0;
while (start < s.size()) {
skipws(s, &start);
if (s[start] == '+') {
start++;
skipws(s, &start);
int end = numend(s, start);
result += atoi(s.substr(start, end-start).c_str());
start = end;
} else if (s[start] == '-') {
start++;
skipws(s, &start, true);
if (s[start] == '(') {
start++;
int end = parenthesisend(s, start);
result -= calculate(s.substr(start, end-start));
start = end+1;
}
else {
int end = numend(s, start);
result -= atoi(s.substr(start, end-start).c_str());
start = end;
}
} else {
int end = numend(s, start);
result = atoi(s.substr(start, end-start).c_str());
start = end;
}
skipws(s, &start);
}
return result;
}
LeetCode224——Basic Calculator的更多相关文章
- LeetCode224. Basic Calculator (用栈计算表达式)
解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...
- [Swift]LeetCode224. 基本计算器 | Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- 数据结构与算法(1)支线任务2——Basic Calculator
题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...
- Basic Calculator
本博客介绍leetcode上的一道不难,但是比较经典的算法题. 题目如下: Implement a basic calculator to evaluate a simple expression s ...
- 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 ...
随机推荐
- u3d中的向量 vector3 vector2
Vector3(x,y,z)x代表左右,y代表上下,z代表前后 Vector3.magnitude 长度 计算两点之间的距离 .如果只给了一点的话.算出的长度其实就是和Vector3.zero点之间 ...
- linux c编程操作数据库(sqlite3应用)
首先pThread 不是linux系统默认库,连接的时候需要使用库libpthread.a. 加入-lpthread参数.另外会有lopen什么找不到的情况.加入-ldl 指定目录.Project_ ...
- Improving Performance【转】
This section provides solutions to some performance problems, and describes configuration best pract ...
- SolrCloud基本过程
转:http://www.data321.com/yunjisuan/20160514880/SolrZhiJieDuQuZKZhongDePeiZhiXin SolrCloud之分布式索引及与Zoo ...
- kali64位 安装 adb
1,adb只有32位的 ,下载地址http://dl.dbank.com/c0umekbpxi# 2,下载解压,但是执行adb命令时,报./adb: error while loading shar ...
- Linux下字符集的安装
目前环境中经常会遇到编码转化的问题,UTF-8跟GB2312也有问题.只得在Linux上安装GB2312(在Linux操作系统上又称zh_CN.GB2312)的字符集,具体请看下文. Linux下几个 ...
- JAVA-JSP内置对象之response对象实现页面跳转
相关资料:<21天学通Java Web开发> response对象 实现页面跳转1.可以通过response对象的sendRedirect()方法设置页面重定向,从而实现页面跳转.2.这种 ...
- [转]MySQL-5.7 Update语句详解
原文地址:https://www.cnblogs.com/tongxiaoda/p/7908977.html .语法 (1)单表 UPDATE [LOW_PRIORITY] [IGNORE] tabl ...
- C语言 · C++中map的用法详解
转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) map<string, int> Map ...
- [shell]shell 中| && || () {} 用法以及shell的逻辑与或非
转自:https://www.jianshu.com/p/617c1ee1e46e | 运算符 管道符号,是unix一个很强大的功能,符号为一条竖线:"|".用法: command ...