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


【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 No.227 Basic Calculator II

LeetCode No.227 Basic Calculator II

Leetcode 227 Basic Calculator II

简单计算器

LeetCode[227] Basic Calculator II

实现一个基本的计算器 https://leetcode.com/problems/basic-calculator-ii/支持简单的’+’,’-‘,’*’,’/’功能。之前在学数据结构的Stack时有...

LeetCode227. Basic Calculator II

题目链接: https://leetcode.com/problems/basic-calculator-ii/ 题目描述:计算字符串表达式的值。可能会有多余空格。题目分析:这道题对于我来说坑略多...
  • codeTZ
  • codeTZ
  • 2016年01月22日 17:50
  • 237

Leetcode 227 Basic Calculator II

1. 问题描述  给遗传字符串类型的表达式,计算表达式的值。表达式只包含+、-、*、/和空格。   2. 方法与思路  此题和Basic Calculator I类似,只是左右括号换成了乘除符号。基...

[Leetcode]Basic Calculator II

Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Basic Calculator & Basic Calculator II
举报原因:
原因补充:

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