[LeetCode] Basic Calculator IV 基本计算器之四

Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1}(given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as ["-1*a","14"]

  • An expression alternates chunks and symbols, with a space separating each chunk and symbol.
  • A chunk is either an expression in parentheses, a variable, or a non-negative integer.
  • A variable is a string of lowercase letters (not including digits.) Note that variables can be multiple letters, and note that variables never have a leading coefficient or unary operator like "2x" or "-x".

Expressions are evaluated in the usual order: brackets first, then multiplication, then addition and subtraction. For example, expression = "1 + 2 * 3" has an answer of ["7"].

The format of the output is as follows:

  • For each term of free variables with non-zero coefficient, we write the free variables within a term in sorted order lexicographically. For example, we would never write a term like "b*a*c", only "a*b*c".
  • Terms have degree equal to the number of free variables being multiplied, counting multiplicity. (For example, "a*a*b*c" has degree 4.) We write the largest degree terms of our answer first, breaking ties by lexicographic order ignoring the leading coefficient of the term.
  • The leading coefficient of the term is placed directly to the left with an asterisk separating it from the variables (if they exist.)  A leading coefficient of 1 is still printed.
  • An example of a well formatted answer is ["-2*a*a*a", "3*a*a*b", "3*b*b", "4*a", "5*c", "-6"]
  • Terms (including constant terms) with coefficient 0 are not included.  For example, an expression of "0" has an output of [].

Examples:

Input: expression = "e + 8 - a + 5", evalvars = ["e"], evalints = [1]
Output: ["-1*a","14"]

Input: expression = "e - 8 + temperature - pressure",
evalvars = ["e", "temperature"], evalints = [1, 12]
Output: ["-1*pressure","5"]

Input: expression = "(e + 8) * (e - 8)", evalvars = [], evalints = []
Output: ["1*e*e","-64"]

Input: expression = "7 - 7", evalvars = [], evalints = []
Output: []

Input: expression = "a * b * c + b * a * c * 4", evalvars = [], evalints = []
Output: ["5*a*b*c"]

Input: expression = "((a - b) * (b - c) + (c - a)) * ((a - b) + (b - c) * (c - a))",
evalvars = [], evalints = []
Output: ["-1*a*a*b*b","2*a*a*b*c","-1*a*a*c*c","1*a*b*b*b","-1*a*b*b*c","-1*a*b*c*c","1*a*c*c*c","-1*b*b*b*c","2*b*b*c*c","-1*b*c*c*c","2*a*a*b","-2*a*a*c","-2*a*b*b","2*a*c*c","1*b*b*b","-1*b*b*c","1*b*c*c","-1*c*c*c","-1*a*a","1*a*b","1*a*c","-1*b*c"]

Note:

  1. expression will have length in range [1, 250].
  2. evalvars, evalints will have equal lengths in range [0, 100].

s

原文地址:https://www.cnblogs.com/grandyang/p/8934427.html

时间: 2024-10-16 02:45:09

[LeetCode] Basic Calculator IV 基本计算器之四的相关文章

解题报告:LeetCode Basic Calculator(简单计算器)

题目出处:https://leetcode.com/problems/basic-calculator/题意描述: 给定一个只含加减号,括号,空格和非负数的合法字符串,在不调用库函数eval的条件下求出其值 解决思路: 由于此题只有加减号,因此不存在运算符优先级的问题,只需要稍微注意一下括号即可. 因此,可以用两个栈ops,numbers来分别存储未处理的运算符和未处理的数字,并对字符串进行一次扫描.对于对于扫描到的任意字符是s[i],做如下处理: 若s[i]为空格,则继续扫描. 若s[i]为左

LeetCode OJ:Basic Calculator(基础计算器)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is

leetcode Basic Calculator

题目连接 https://leetcode.com/problems/basic-calculator/ Basic Calculator Description Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non

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

Description: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given e

[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

[Swift]LeetCode770. 基本计算器 IV | Basic Calculator IV

Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {"e": 1} (given in terms of evalvars = ["e"] and evalints = [1]), return a list of tokens representing the simplified expression, such as 

[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() Basic Calculator 不知道哪里错了

class Solution {public:    int calculate(string s) {        stack<int> num;        stack<char> symbol;        for(int i=0;i<s.length();i++){            if(s[i]==' ')   continue;            else if(s[i]=='('||s[i]=='+'||s[i]=='-')  symbol.pu