# Basic Calculator

Description:

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.

# Basic Calculator II

Description:

mplement 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 given expression is always valid.

Solution:

import java.util.*;

public class Solution {

public int calculate(String s) {
s = s + " ";
toReversePolishNotation(s, list);
System.out.println(list.toString());
return calcReversePolishNotation(list);
}

void toReversePolishNotation(String s, LinkedList<String> list) {
Stack<Character> operation = new Stack<Character>();
operation.push('(');
int preNum = 0;

char ch;
int currentLevel;
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
if (ch >= '0' && ch <= '9') {
preNum = preNum * 10 + ch - '0';
if (s.charAt(i + 1) < '0' || s.charAt(i + 1) > '9') {
preNum = 0;
}
} else {
if (ch == '(') {
operation.push(ch);
} else if (ch == ')') {
char tempC;
while (!operation.isEmpty()) {
tempC = operation.pop();
if (tempC == '(')
break;
}
} else if (ch != ' ') {
currentLevel = getLevel(ch);

char tempC;
while (!operation.isEmpty()) {
tempC = operation.peek();
if (getLevel(tempC) < currentLevel)
break;
operation.pop();
}
operation.push(ch);
}
}
}

char tempC;
while (operation.size() > 1) {
tempC = operation.pop();
}

return;
}

int getLevel(char ch) {
if (ch == '(')
return 0;
if (ch == '+')
return 1;
if (ch == '-')
return 1;
if (ch == '*')
return 2;
if (ch == '/')
return 2;
return 0;
}

Iterator<String> ite = list.iterator();
Stack<Integer> stack = new Stack<Integer>();
String temp;
int second, first;
while (ite.hasNext()) {
temp = ite.next();
if (temp.equals("+")) {
second = stack.pop();
first = stack.pop();
stack.push(first + second);
} else if (temp.equals("-")) {
second = stack.pop();
first = stack.pop();
stack.push(first - second);
} else if (temp.equals("*")) {
second = stack.pop();
first = stack.pop();
stack.push(first * second);
} else if (temp.equals("/")) {
second = stack.pop();
first = stack.pop();
stack.push(first / second);
} else {
stack.push(Integer.parseInt(temp));
}
}

return stack.pop();
}

public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.calculate("1-2+3"));
System.out.println(s.calculate("1-(2+3)"));
System.out.println(s.calculate("2*4-2*3"));
}
}

• 本文已收录于以下专栏：

## 【LeetCode】Basic Calculator && Basic Calculator II

1、Basic Calculator  Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution  ...
• u013027996
• 2015年06月24日 11:01
• 3511

## LeetCode 227. Basic Calculator II 解题报告【python】

• kuaisuzhuceh
• 2016年01月30日 14:11
• 605

## LeetCode[227] Basic Calculator II

• hust_dxxxd
• 2016年05月04日 11:29
• 377

## leetcode 227: Basic Calculator II

leetcode 227: Basic Calculator II PYTHON JAVA C++
• xudli
• 2015年06月26日 03:18
• 6894

## Leetcode 224: Basic Calculator

Leetcode 224: Basic Calculator java c++ python
• xudli
• 2015年06月19日 00:59
• 7579

## LeetCode 770. Basic Calculator IV题目解析

• cqx5555
• 2018年01月26日 12:47
• 140

## LeetCode-227. Basic Calculator II (JAVA)实现计算器 II

LeetCode-227. Basic Calculator II (JAVA)实现计算器 II
• mine_song
• 2017年04月30日 09:54
• 446

## 【LeetCode】Basic Calculator 解题报告

【题目】 Implement a basic calculator to evaluate a simple expression string. The expression str...
• ljiabin
• 2015年06月14日 11:19
• 8273

## 224. Basic Calculator | Java最短代码实现

• happyaaaaaaaaaaa
• 2016年03月20日 15:12
• 802

## LeetCode 224 Basic Calculator (后缀表达式 栈)

LeetCode 224 Basic Calculator (后缀表达式 栈)
• Tc_To_Top
• 2016年08月28日 23:45
• 843

举报原因： 您举报文章：Basic Calculator & Basic Calculator II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)