Basic Calculator & Basic Calculator II

原创 2015年07月11日 08:59:28

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) {
		LinkedList<String> list = new LinkedList<String>();
		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') {
					list.add(preNum + "");
					preNum = 0;
				}
			} else {
				if (ch == '(') {
					operation.push(ch);
				} else if (ch == ')') {
					char tempC;
					while (!operation.isEmpty()) {
						tempC = operation.pop();
						if (tempC == '(')
							break;
						list.add(tempC + "");
					}
				} else if (ch != ' ') {
					currentLevel = getLevel(ch);

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

		char tempC;
		while (operation.size() > 1) {
			tempC = operation.pop();
			list.add(tempC + "");
		}

		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;
	}

	int calcReversePolishNotation(LinkedList list) {
		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"));
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Basic Calculator II

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

【leetcode】String——Basic Calculator II (227)

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

[LeetCode]Basic Calculator II

解题思路: 1,就是要写个简单的计算器呗 2,先把所有的 space去掉, 实现nonSpace方法; 3,把所有的 operator 和 operands分开,实现简单的 splitToOpe...

Basic Calculator II -- leetcode

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

LeetCode 227. Basic Calculator II

LeetCode 227. Basic Calculator II 时间复杂度O(n),空间复杂度O(1)c++

[LeetCode] Basic Calculator II

Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The e...

LeetCode OJ Basic Calculator II

Basic Calculator II题目思路和这个一样:Basic Calculator I代码class ExpressionTransformation { public: string...

Leetcode 227. Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/#/description 题目描述: mplement a basic ca...

leetcode:Basic Calculator II

leetcode 简易计算器的实现

227-m-Basic Calculator II

四则运算。真的只是四则而已,没有要求括号只有加减乘除,因此还算是简单的,难点可能就在对乘除的处理上。最开始写的是将操作数和操作符全部分别提取到2个数组中,再一个个出栈运算,发现不好写,因为数组是静态栈...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)