Basic Calculator 基本计算器

2018-09-27 22:02:36

一、Basic Calculator II

问题描述:

问题求解:

sign用来保存前一个符号,用num来记录数字,如果碰到一个符号或者到达结尾,则需要进行入栈操作,这个时候需要结合符号进行相应的运算。

    public int calculate(String s) {
        if (s == null || s.length() == 0) return 0;
        int res = 0;
        Stack<Integer> stack = new Stack<>();
        char sign = ‘+‘;
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.charAt(i))) {
                num = num * 10 + s.charAt(i) - ‘0‘;
            }
            if (!Character.isDigit(s.charAt(i)) && s.charAt(i) != ‘ ‘ || i == s.length() - 1) {
                if (sign == ‘/‘) num = stack.pop() / num;
                if (sign == ‘*‘) num = stack.pop() * num;
                if (sign == ‘-‘) num *= -1;
                stack.push(num);
                sign = s.charAt(i);
                num = 0;
            }
        }
        while (!stack.isEmpty()) res += stack.pop();
        return res;
    }

二、Basic Calculator

问题描述:

问题求解:

本问题中只包含了+,-和括号,首先如果只有+,-的话连Stack都不需要就可以直接遍历一遍相加就可以了。

但是如果存在括号的情况,那么就需要使用到一个Stack来对之前计算到的数字和符号进行保存。

    public int calculate(String s) {
        if (s == null || s.length() == 0) return 0;
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        int sign = 1;
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) num = num * 10 + c - ‘0‘;
            else if (c == ‘+‘) {
                res += num * sign;
                sign = 1;
                num = 0;
            }
            else if (c == ‘-‘) {
                res += num * sign;
                sign = -1;
                num = 0;
            }
            else if (c == ‘(‘) {
                stack.push(res);
                stack.push(sign);
                res = 0;
                sign = 1;
            }
            else if (c == ‘)‘) {
                res += num * sign;
                res *= stack.pop();
                res += stack.pop();
                num = 0;
            }
        }
        if (num != 0) res += num * sign;
        return res;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9715959.html

时间: 2024-08-01 17:29:44

Basic Calculator 基本计算器的相关文章

Basic Calculator 基本计算器-Leetcode

1.题目: 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 expressi

LeetCode OJ: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

[Swift]LeetCode227. 基本计算器 II | Basic Calculator II

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" Ou

LeetCode 227. 基本计算器 II(Basic Calculator II)

227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和空格.整数除法仅保留整数部分. LeetCode227. Basic Calculator II中等 示例?1: 输入: "3+2*2" 输出: 7 示例 2: 输入: " 3/2 " 输出: 1 示例 3: 输入: " 3+5 / 2 " 输出:

227. Basic Calculator II 无括号版本计算器

[抄题]: 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&q

Basic Calculator - Stack(表达式计算器)

978. Basic Calculator https://www.lintcode.com/problem/basic-calculator/description public class Solution { /** * @param s: the given expression * @return: the result of expression */ public int calculate(String s) { // Write your code here Stack<Int

[LeetCode-JAVA] Basic Calculator II

题目: 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. You may assume that the give

[LeetCode-JAVA] 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

LeetCode——Basic Calculator II

Description: 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. You may assume that