241. Different Ways to Add Parentheses

本文介绍了一种使用分治法解决字符串中数字与运算符的不同组合方式的问题,旨在找出所有可能的运算结果。通过递归地将输入字符串分为左右两部分并计算所有可能的组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

241. Different Ways to Add Parentheses


Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.

Example 1
Input: "2-1-1".

((2-1)-1) = 0
(2-(1-1)) = 2

Output: [0, 2]

Example 2
Input: "2*3-4*5"

(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10

Output: [-34, -14, -10, -10, 10]

题目大意

对input的string,添加括号改变运算顺序,计算出所有可能的值。
PS: 经过尝试后,输出顺序不会影响正确性。

解题思路

利用分治法,以符号位为分界点,将input截成左右两段,递归计算,并将结果push进列表result尾部。
PS: 如果input不含符号,则将其转换为int格式,push进列表result。

算法复杂度

遍历input寻找符号的复杂度:O(n)
计算每种可能顺序结果的复杂度:O(logn)
总复杂度:O(nlogn)

代码实现

class Solution {
public:
  vector<int> diffWaysToCompute(string input) {
    vector<int> result;
    int length= input.size();
    for (int i = 0; i < length; i++) {
      // 以+, -, * 为‘分’的标志
      if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
        vector<int> left = diffWaysToCompute(input.substr(0, i));
        vector<int> right = diffWaysToCompute(input.substr(i+1));
        vector<int>::iterator j, k;
        for (j = left.begin(); j != left.end(); j++) {
          for (k = right.begin(); k != right.end(); k++) {
            switch(input[i]) {
              case '+':
                result.push_back((*j)+(*k));
                break;
              case '-':
                result.push_back((*j)-(*k));
                break;
              case '*':
                result.push_back((*j)*(*k));
            }
          }
        }
      }
    }
    // input不含符号的情况
    if (result.empty()) {
      result.push_back(atoi(input.c_str()));
    }
    return result;
  }
};
在使用 `env.py` 时,如果遇到 `Missing parentheses in call to 'print'` 错误,这通常是由于代码中使用了 Python 2 的 `print` 语法,而当前运行环境是 Python 3。Python 3 中的 `print` 是一个函数,因此必须使用括号来包裹输出内容。 例如,Python 2 中的写法: ```python print "Hello, world!" ``` 在 Python 3 中应该改为: ```python print("Hello, world!") ``` ### 解决方法 1. **修改 `env.py` 文件中的 `print` 语句** 找到文件中所有未使用括号的 `print` 语句,并将其更改为函数调用形式。例如,将: ```python print readerRef ``` 改为: ```python print(readerRef) ``` [^1] 2. **检查文件中的所有 `print` 语句** 确保文件中所有 `print` 语句都符合 Python 3 的语法要求,避免遗漏。例如: ```python print "Cannot run setup when server is running." ``` 应该修改为: ```python print("Cannot run setup when server is running.") ``` [^3] 3. **确认运行环境的 Python 版本** 如果项目原本是为 Python 2 编写的,建议检查所有依赖库是否兼容 Python 3。如果不兼容,可以考虑使用虚拟环境运行 Python 2,或者对代码进行迁移以适配 Python 3。 4. **使用自动化工具进行语法转换** 可以使用 `2to3` 工具自动将 Python 2 代码转换为 Python 3 代码。例如: ```bash 2to3 -w env.py ``` 这将自动修复 `print` 语句等不兼容问题。 5. **确保文件具有可执行权限(如涉及脚本执行)** 如果 `env.py` 是一个可执行脚本,确保它具有正确的执行权限: ```bash chmod +x env.py ``` 此外,检查文件的第一行是否指定了正确的解释器,例如: ```python #!/usr/bin/env python3 ``` 通过以上方法,可以有效解决 `env.py` 中出现的 `Missing parentheses in call to 'print'` 错误。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值