博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#227] Basic Calculator II
阅读量:4662 次
发布时间:2019-06-09

本文共 3028 字,大约阅读时间需要 10 分钟。

Problem:

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

 

Note: Do not use the eval built-in library function.

 

Analysis:

Unlike the Calculator 1, this quesion is acutally much easier!!! You should do through a very tricky method.To understand this problem, we should firstly understand the puzzle around this problem.for equation like "22 - 31 * 52 + 22"when we reach 31, we do not know whether to use it with 22, thus "22 - 31" or keep it for late usage.In this situation, we apparenlty should not use it with 22 as "22 - 31", but use it with 52 as "31 * 52".But we really could not know such information, until we reach "*" and get "52". Can we delay such calculation?Yes!!!!Why not decide the usage of 31 until we reach "+" after 52, when we have already know "*" and "52". We can do it!!!!Suppose when reach a sign character "+ - * /", we have already know the number and the sign right before the number. (sign) num---------------------------------------------------------------------------------------------------------if (sign == '+')    stack.push(num);if (sign == '-')    stack.push(-1 * num);if (sign == '*')    stack.push(stack.pop() * num);if (sign == '/')    stack.push(stack.pop() / num);iff the sign is '*' or '/', we combine the preivous two numbers together. " (-31) * 52" as single number. (Just like we do the calculation manually. Right!!!)----------------------------------------------------------------------------------------------------------Sklls:1. how to get the num?char c = s.charAt(i);if (Character.isDigit(c))     num = num*10 + (c - '0');Note: Character.isDigit(c) is very elegant!2. how to tackle the last num?if (i == s.length() - 1) {...}3. how to get the number before the current num.stack.pop()

Solution:

public class Solution {    public int calculate(String s) {        if (s == null || s.length() == 0)            return 0;        Stack
stack = new Stack
(); int num = 0; char sign = '+'; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isDigit(c)) num = num*10 + (c - '0'); if ((c != ' ' && !Character.isDigit(c)) || i == s.length() - 1) { if (sign == '+') stack.push(num); if (sign == '-') stack.push(-1 * num); if (sign == '*') stack.push(stack.pop() * num); if (sign == '/') stack.push(stack.pop() / num); sign = c; num = 0; } } int ret = 0; for (int i : stack) { ret += i; } return ret; }}

 

转载于:https://www.cnblogs.com/airwindow/p/4824721.html

你可能感兴趣的文章
代理的四种实现方式
查看>>
12-29 注册审核
查看>>
计算一个算数表达式的值
查看>>
hdu squarefree number
查看>>
atc-前端模板预编译器
查看>>
poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
查看>>
Codeforces Round #194 (Div. 1) A. Secrets 数学
查看>>
看不懂 ASP.NET 相册上传代码
查看>>
redis+mysql
查看>>
IIS中找不到dll文件的依赖项问题
查看>>
Loadrunner的stock和web协议对应的事务检查点
查看>>
mvc扩展HtmlHelper功能
查看>>
codeforces #541 D. Gourmet choice(拓扑+并查集)
查看>>
ocilib linux编译安装
查看>>
linux 链接库说明
查看>>
基于本地文件系统的LocalDB
查看>>
黑马程序员 java基础加强--类加载器
查看>>
Win10环境下安装Django
查看>>
[Leetcode] Permutations
查看>>
mysqlbinlog flashback 5.6完全使用手册与原理
查看>>