leetcode-Basic Calculator II-227

和上题差不多http://www.cnblogs.com/0summer/p/5837634.html

输入一个字符串,代表一个表达式,包含的运算符有+,-,*,/

本题没有包括括号,我的做法是把数字和+或者-压栈,继续遍历,如果遇到*或者/取出栈顶的数字和*或者/之后的数字进行计算然后再压栈,最后栈中剩下的就是所有+或者-表达式,再顺序计算一遍即可。

注释输入的字符串可能包含空格,要处理下

 1 class Solution {
 2 public:
 3     int calculate(string s) {
 4         int len=s.size();
 5         if(len==0) return 0;
 6         stack<int> st;
 7         int i=0;
 8         int flag=1;
 9         while(i<len){
10             if(s[i]==‘ ‘){
11                 i++;
12                 continue;
13             }
14             if(s[i]==‘+‘){
15                 flag=1;
16                 st.push(flag);
17             }
18             else if(s[i]==‘-‘){
19                 flag=-1;
20                 st.push(flag);
21             }
22             else if(s[i]==‘*‘||s[i]==‘/‘){
23                 char c=s[i];
24                 i++;
25                 int num=0;
26                 while(i<len&&s[i]==‘ ‘) i++;
27                 while(i<len&&isdigit(s[i])){
28                     num=num*10+s[i]-‘0‘;
29                     i++;
30                 }
31                 int tmp=st.top();
32                 st.pop();
33                 if(c==‘*‘) tmp*=num;
34                 else tmp/=num;
35                 st.push(tmp);
36                 continue;
37             }
38             else if(isdigit(s[i])){
39                 int num=0;
40                 while(i<len&&isdigit(s[i])){
41                     num=num*10+s[i]-‘0‘;
42                     i++;
43                 }
44                 st.push(num);
45                 continue;
46             }
47             i++;
48         }
49         int cnt=0;
50         vector<int> ans;
51         while(!st.empty()){
52             cnt++;
53             ans.push_back(st.top());
54             st.pop();
55         }
56         int ret=ans[cnt-1];
57         i=cnt-2;
58         while(i>0){
59             ret+=ans[i]*ans[i-1];
60             i-=2;
61         }
62         return ret;
63     }
64 };
时间: 2024-12-15 03:05:05

leetcode-Basic Calculator II-227的相关文章

LeetCode——Basic Calculator II

Description: 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

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

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 ex

[LeetCode] Basic Calculator II

The basic idea of is as follows: Maintain a deque operands for the numbers and another deque operations for the operators +, -, *,/`. Scan the expression from left to right, each time we meet a digit, extract the whole number with and after it, push

LeetCode 227. 基本计算器 II(Basic Calculator II)

227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和空格.整数除法仅保留整数部分. LeetCode227. Basic Calculator II中等 示例?1: 输入: "3+2*2" 输出: 7 示例 2: 输入: " 3/2 " 输出: 1 示例 3: 输入: " 3+5 / 2 " 输出:

【LeetCode】227. Basic Calculator II

Basic Calculator II 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 assu

LeetCode OJ Basic Calculator II

Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式 long long int calculate_from_postfix_expression(); // 根据后缀表达式计算值 private: vector<string

Leetcode 227. Basic Calculator II JAVA语言

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 ex

[LeetCode#227] Basic Calculator II

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

Java for LeetCode 227 Basic Calculator II

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 ex

leetcode 227. Basic Calculator II ---------- java

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 ex