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

```	static public int calculate(String s) {
int res = 0, tempRes = 0, Option = 1, tempOp = 1;
for (int i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case ' ':
break;
case '+':
Option = 1;
break;
case '-':
Option = -1;
break;
case '*':
Option = 2;
break;
case '/':
Option = 3;
break;
default:
int num = 0;
while (i < s.length() && Character.isDigit(s.charAt(i)))
num = num * 10 + s.charAt(i++) - '0';
i--;
switch (Option) {
case 1:
res += tempOp * tempRes;
tempRes = num;
tempOp = 1;
break;
case -1:
res += tempOp * tempRes;
tempRes = num;
tempOp = -1;
break;
case 2:
tempRes *= num;
break;
case 3:
tempRes /= num;
}

}
}
res += tempOp * tempRes;
return res;
}
```

## Java for LeetCode 227 Basic Calculator II的更多相关文章

1. LeetCode#227.Basic Calculator II

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

2. (medium)LeetCode 227.Basic Calculator II

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

3. Java for LeetCode 224 Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

4. 227. Basic Calculator II

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

5. Basic Calculator,Basic Calculator II

一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...

6. LeetCode Basic Calculator II

原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

7. [LeetCode] Basic Calculator II 基本计算器之二

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

8. Basic Calculator II

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

9. Java for LeetCode 212 Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

## 随机推荐

1. Oracle 释放flash recovery area的四种方法

早上收到一台Linux服务器磁盘告警邮件以及监控告警日志程序发来的邮件.检查过后,发现Linux服务器中一个分区没有空间了.主要原因是由于昨晚程序员做升级时,产生了大量的归档日志,导致联机重做日志无法 ...

2. myeclipse2015CI Server显示derby服务器去除方法

找到myeclipse的安装目录 myeclipse2015CI\configuration\org.eclipse.equinox.simpleconfigurator 打开文件bundles.in ...

3. ASP.NET MVC系列:控制器的Edit方法

在前面我们已经介绍了如何创建控制器.视图和数据模型,运行之前的项目并打开Movies主页,将鼠标悬停在“编辑”菜单上,你会看到浏览器右下角显示了“编辑”链接的地址

4. SQL Server 2000 sp2 及更低版本不受此版本的 Windows 支持

SQL Server 2000 sp2 及更低版本不受此版本的 Windows 支持.在安装了 SQL Server 2000 之后请应用 sp3. 出现这种现象的原因在于:Windows Serve ...

5. android Eclipse执行项目提示错误： unable to execute dex: GC orerhead limit exceeded

Eclipse执行项目提示错误: unable to execute dex: GC orerhead limit exceeded 解决方法: 找到Eclipse安装目录的文件,\eclipse\e ...

6. KVM 介绍（1）：简介及安装

学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...

7. mysql 在linux 修改账号密码

1.root用户登录到mysql数据库代码示例:/usr/local/mysql/bin/mysql -u root -p (输入密码进入mysql)2.进入mysql,输入:代码示例:use mys ...

8. HDU 5145 NPY and girls 莫队+逆元

NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ...

9. 【转】MSM搭建（Memcached_Session_Manager）--解决集群session共享

一.环境     tomcat7三台,nginx(负载均衡),memcached(1.4.0) 需要的jar 二.memcached搭建     需要安装libevent 三.tomcat配置 在to ...

10. Apache-Maven学习（转）

之前听同学说过关于他们公司使用Maven框架来做项目管理,后来也跟他一起参与过一个小的项目,但没有仔细的去搞,现在好好学习一下. 文章转载自:易百教程 [http:/www.yiibai.com] A ...