[LeetCode 第4题] -- Evaluate Reverse Polish Notation

题目链接: Evaluate Reverse Polish Notation

题目意思: 给的一个表达式,求值

代码:

class Solution {
public:
     bool IsOperator(const string &str);
     int GetStackTop(stack<int> &stack);
     int evalRPN(vector<string> &tokens);
};

bool Solution::IsOperator(const string &str) {
     if (str == "+" || str == "-" ||
         str == "*" || str == "/") {
         return true;
     }
     return false;
}

int Solution::GetStackTop(stack<int> &stack) {
     if (stack.empty()) {
         throw "stack is empty";
         return -1;
     }
     int topValue = stack.top();
     stack.pop();
     return topValue;
}

int Solution::evalRPN(vector<string> &tokens) {
     stack <int> number;
     int tokensSize = tokens.size();
     for (int i = 0; i < tokensSize; ++i) {
         if (IsOperator(tokens[i])) {
             int rightNumber = GetStackTop(number);
             int leftNumber = GetStackTop(number);
             int result = 0;
             if (tokens[i] == "+") {
                result = leftNumber + rightNumber;
             }
             else if (tokens[i] == "-") {
                result = leftNumber - rightNumber;
             }
             else if (tokens[i] == "*") {
                result = leftNumber * rightNumber;
             }
             else {
                result = leftNumber / rightNumber;
             }
             number.push(result);
         }
         else {
             number.push(atoi(tokens[i].c_str()));
         }
      }
      return GetStackTop(number);
}
时间: 2024-11-10 12:02:21

[LeetCode 第4题] -- Evaluate Reverse Polish Notation的相关文章

[LeetCode] Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

编程-leetcode上的一题,Reverse Bits?

问题描述 leetcode上的一题,Reverse Bits? 1C 题目如下:Reverse bits of a given 32 bits unsigned integer.For example given input 43261596 (represented in binary as 00000010100101000001111010011100) return 964176192 (represented in binary as00111001011110000010100101

[LeetCode 第3题] -- Reverse Words in a String

题目链接: Reverse Words in s String 题目意思: 给定一个字符串,要求把字符串中的"单词"反转                 1. 所谓"单词"指的的连续的非空白字符                 2. 必须把前后缀空格去掉                 3. 连续多个空格要合并为一个 代码 class Solution { public: void reverseWords(string &s); }; void Solut

[leetCode 第2题] -- Maximum Product Subarray

题目链接 题目意思: 给定一个数组,求最大连续子序列的乘积 分析: 简单dp.maxDp[i]表示以num[i]结尾的最大子序列乘积,minDp[i]类似          maxDp[i] = max{num[i], maxDp[i-1]*num[i], minDp[i-1]*num[i]}          minDp[i] = min{num[i], minDp[i-1]*num[i], maxDp[i-1]*num[i]}          之所以要minDp,是因为有负数的存在,导致多

[LeetCode 第10题] -- Linked List Cycle

题目链接: linked List Cycle 题目意思: 给定一个链表,判断链表是否有环 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head); }; b

[LeetCode 第11题] -- Linked List Cycle II

题目链接: Linked List Cycle II 题目意思: 给定一个链表,如果链表有环求出环的起点,否则返回NULL 解题思路:      1. 判断链表是否有环: 两个指针,一个一次走一步,一个一次走两步,如果指针相遇说明有环,否则无环.     2. 如果有环的情况下,我们可以画个图(图片来自网络)                   假设两个指针在z点相遇.则          a. 指针1走过路程为a + b:指针2走过的路程为 a+b+c+b          b. 因为指针2的

[leetCode 第1题] -- Find Minimum in Rotated Sorted Array

题目链接: Find Minimun in Rotated Sorted Array 题目意思: 给定一个旋转数组,找出这个旋转数组的最小元素.旋转数组的定义是,把一个从小到大排好序的数组,取一部份放到末尾,例如0 1 2 4 5 6 7 取 0 1 2放到末尾,变成旋转数组4 5 6 7 0 1 2 代码:  class Solution { public: int findMin(vector<int> &num); }; int Solution::findMin(vector&

[LeetCode 第8题] -- Binary Tree Preorder Traversal

题目链接: Binary Tree Preorder Traversal 题目意思: 给定一个二叉树根节点,求前序序列 代码: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { publi