LeetCode -- Basic Calculator II

原创 2015年11月21日 09:54:29
题目描述:


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


Some examples:
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5


就是对1个表达式解析出操作数和操作符,做四则运算,但不包括括号。


思路:
1. 去除空白字符
2. 考虑多位数字的情况
3. 如果是'*'或'/',直接计算;否则入栈
4. 由于只剩下加减运算符了,按顺序计算出结果即可。




实现代码:



public int Calculate(string s)
{
	var exp = s.Where(c => c != ' ').ToList();
	
	var stackOp = new Stack<char>();
	var stackNum = new Stack<int>();
	
	var ops = new []{'+','-','*','/'};
	
	var n = "";
	for(var i = 0;i < exp.Count; i++){
		if(ops.Contains(exp[i])) // detected operator
		{
			stackOp.Push(exp[i]);
		}
		else{
			// parse the number
			for(var j = i;j < exp.Count; j++){
				if(!ops.Contains(exp[j])){
					n += exp[j];
				}
				else{
					break;
				}
			}
			
			var n1 = int.Parse(n);
			// if top is '*' or '/' , immediately calculate
			if(stackOp.Count > 0 && (stackOp.Peek() == '*' || stackOp.Peek() == '/')){
				var n2 = stackNum.Pop();
				stackNum.Push(Calc(n2, n1, stackOp.Pop()));
			}
			else{
				stackNum.Push(n1);
			}
			
			i += n.Length - 1;
			n = "";
		}
	}
	
	// since now we only left '+' and '-'
	// we should reverse the order (both operators and numbers) back to where they were
	var stackOp1 = stackOp.Reverse();
	var stackNum1 = new Stack<int>(stackNum);
	var count = 0;
	
	foreach(var op in stackOp1){
		var n1 = stackNum1.Pop();
		var n2 = stackNum1.Pop();
		
		stackNum1.Push(Calc(n1 , n2, op));
		count += 2;
	}
	
	return stackNum1.Pop();
}


private int Calc(int n1 , int n2, char op)
{
	switch(op){
		case '+':
			return n1 + n2;
		case '-':
			return n1 - n2;
		case '*':
			return n1 * n2;
		case '/':
			return n1 / n2;
		default :
		throw new ArgumentException(string.Format("unexpected operator : {0}", op));
	}
}


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

相关文章推荐

Leetcode之Basic Calculator & Basic Calculator II

Leetcode之basic calculator、basic calculator II

【LeetCode】Basic Calculator II

【LeetCode】Basic Calculator II Implement a basic calculator to evaluate a simple expression string. ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

Basic Calculator II -- leetcode

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

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/题目: Implement a basic calculator to evaluat...

[LeetCode]Basic Calculator II

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

《leetCode》:Basic Calculator II

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

[LeetCode] Basic Calculator II

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

[Leetcode]Basic Calculator II

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

Leetcode 227. Basic Calculator II

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

Leetcode 227. Basic Calculator II

Problem mplement a basic calculator to evaluate a simple expression string. The expression string...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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