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的更多相关文章

  1. LeetCode224. Basic Calculator (用栈计算表达式)

    解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...

  2. [Swift]LeetCode224. 基本计算器 | Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  4. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  6. 数据结构与算法(1)支线任务2——Basic Calculator

    题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...

  7. Basic Calculator

    本博客介绍leetcode上的一道不难,但是比较经典的算法题. 题目如下: Implement a basic calculator to evaluate a simple expression s ...

  8. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  9. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

随机推荐

  1. 基于OCS实现高速缓存

    OCS简介 OCS( Open Cache Service)为分布式高速缓存服务,主要实现热点数据的快速响应: OCS支持Key/Value的数据结构,兼容memcachebinary protoco ...

  2. java向mysql插入数据乱码

    修改jdbc的链接,将原来的         jdbc:mysql://localhost:3306/demo改为        jdbc:mysql://localhost:3306/demo?us ...

  3. pyspark 编写 UDF函数

    pyspark 编写 UDF函数 前言 以前用的是Scala,最近有个东西要用Python,就查了一下如何编写pyspark的UDF. pyspark udf 也是先定义一个函数,例如: def ge ...

  4. Python3回文相关算法小结

    [本文出自天外归云的博客园] 总结一下关于回文相关的算法: 判断字符串本身是否是回文 返回字符串中的所有子串 找到字符串中包含的所有回文 判断字符串中是否包含回文 将字符串变成一个不包含回文的字符串 ...

  5. RTX基础教程目录

    以下RTX教程转载自安富莱电子论坛: http://forum.armfly.com/forum.php?mod=viewthread&tid=16909&extra=page%3D1 ...

  6. 判断URL文件是不是在于在。

    判断URL文件是不是在于在. private static bool UrlIsExist(string url) { System.Uri u = null; try { u = new Uri(u ...

  7. 基于html5海贼王单页视差滚动特效

    分享一款基于html5海贼王单页视差滚动特效是一款流行滑落网页特效代码.效果图如下: 在线预览   源码下载 实现的代码: <div class="top"> < ...

  8. 结合order by 解CTF某题

    真tmd不容易 <?php error_reporting(0); if (!isset($_POST['uname']) || !isset($_POST['pwd'])) { echo '& ...

  9. Linux使用redis

    在linux遇到这种情况. 注意,这里本redis 用的端口是6389 通过 ps -aux 看到redis 启动了: root ? Ssl Jun14 : redis-server *: root ...

  10. [转]使用Navicat导入导出数据库表

    原文地址:https://blog.csdn.net/anselandevil/article/details/81667199 步骤1:数据中原始数据如下: 点击表,右键选择导出向导,选择导出为sq ...