返回介绍

solution / 0200-0299 / 0241.Different Ways to Add Parentheses / README_EN

发布于 2024-06-17 01:04:02 字数 6865 浏览 0 评论 0 收藏 0

241. Different Ways to Add Parentheses

中文文档

Description

Given a string expression of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in any order.

The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed 104.

 

Example 1:

Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0 
(2-(1-1)) = 2

Example 2:

Input: expression = "2*3-4*5"
Output: [-34,-14,-10,-10,10]
Explanation:
(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

 

Constraints:

  • 1 <= expression.length <= 20
  • expression consists of digits and the operator '+', '-', and '*'.
  • All the integer values in the input expression are in the range [0, 99].

Solutions

Solution 1

class Solution:
  def diffWaysToCompute(self, expression: str) -> List[int]:
    @cache
    def dfs(exp):
      if exp.isdigit():
        return [int(exp)]
      ans = []
      for i, c in enumerate(exp):
        if c in '-+*':
          left, right = dfs(exp[:i]), dfs(exp[i + 1 :])
          for a in left:
            for b in right:
              if c == '-':
                ans.append(a - b)
              elif c == '+':
                ans.append(a + b)
              else:
                ans.append(a * b)
      return ans

    return dfs(expression)
class Solution {
  private static Map<String, List<Integer>> memo = new HashMap<>();

  public List<Integer> diffWaysToCompute(String expression) {
    return dfs(expression);
  }

  private List<Integer> dfs(String exp) {
    if (memo.containsKey(exp)) {
      return memo.get(exp);
    }
    List<Integer> ans = new ArrayList<>();
    if (exp.length() < 3) {
      ans.add(Integer.parseInt(exp));
      return ans;
    }
    for (int i = 0; i < exp.length(); ++i) {
      char c = exp.charAt(i);
      if (c == '-' || c == '+' || c == '*') {
        List<Integer> left = dfs(exp.substring(0, i));
        List<Integer> right = dfs(exp.substring(i + 1));
        for (int a : left) {
          for (int b : right) {
            if (c == '-') {
              ans.add(a - b);
            } else if (c == '+') {
              ans.add(a + b);
            } else {
              ans.add(a * b);
            }
          }
        }
      }
    }
    memo.put(exp, ans);
    return ans;
  }
}
class Solution {
public:
  vector<int> diffWaysToCompute(string expression) {
    return dfs(expression);
  }

  vector<int> dfs(string exp) {
    if (memo.count(exp)) return memo[exp];
    if (exp.size() < 3) return {stoi(exp)};
    vector<int> ans;
    int n = exp.size();
    for (int i = 0; i < n; ++i) {
      char c = exp[i];
      if (c == '-' || c == '+' || c == '*') {
        vector<int> left = dfs(exp.substr(0, i));
        vector<int> right = dfs(exp.substr(i + 1, n - i - 1));
        for (int& a : left) {
          for (int& b : right) {
            if (c == '-')
              ans.push_back(a - b);
            else if (c == '+')
              ans.push_back(a + b);
            else
              ans.push_back(a * b);
          }
        }
      }
    }
    memo[exp] = ans;
    return ans;
  }

private:
  unordered_map<string, vector<int>> memo;
};
var memo = map[string][]int{}

func diffWaysToCompute(expression string) []int {
  return dfs(expression)
}

func dfs(exp string) []int {
  if v, ok := memo[exp]; ok {
    return v
  }
  if len(exp) < 3 {
    v, _ := strconv.Atoi(exp)
    return []int{v}
  }
  ans := []int{}
  for i, c := range exp {
    if c == '-' || c == '+' || c == '*' {
      left, right := dfs(exp[:i]), dfs(exp[i+1:])
      for _, a := range left {
        for _, b := range right {
          if c == '-' {
            ans = append(ans, a-b)
          } else if c == '+' {
            ans = append(ans, a+b)
          } else {
            ans = append(ans, a*b)
          }
        }
      }
    }
  }
  memo[exp] = ans
  return ans
}
using System.Collections.Generic;

public class Solution {
  public IList<int> DiffWaysToCompute(string input) {
    var values = new List<int>();
    var operators = new List<char>();
    var sum = 0;
    foreach (var ch in input)
    {
      if (ch == '+' || ch == '-' || ch == '*')
      {
        values.Add(sum);
        operators.Add(ch);
        sum = 0;
      }
      else
      {
        sum = sum * 10 + ch - '0';
      }
    }
    values.Add(sum);

    var f = new List<int>[values.Count, values.Count];
    for (var i = 0; i < values.Count; ++i)
    {
      f[i, i] = new List<int> { values[i] };
    }

    for (var diff = 1; diff < values.Count; ++diff)
    {
      for (var left = 0; left + diff < values.Count; ++left)
      {
        var right = left + diff;
        f[left, right] = new List<int>();
        for (var i = left; i < right; ++i)
        {
          foreach (var leftValue in f[left, i])
          {
            foreach (var rightValue in f[i + 1, right])
            {
              switch (operators[i])
              {
                case '+':
                  f[left, right].Add(leftValue + rightValue);
                  break;
                case '-':
                  f[left, right].Add(leftValue - rightValue);
                  break;
                case '*':
                  f[left, right].Add(leftValue * rightValue);
                  break;
              }
            }
          }
        }
      }
    }

    return f[0, values.Count - 1];
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文