LeetCode 241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses

Description Submission Solutions

  • Total Accepted: 38849
  • Total Submissions: 92740
  • Difficulty: Medium
  • Contributors: Admin

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]

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.


【题目分析】

给定一个算术表达,给这个计算这个表达式可能的所有的结果。在表达式中可以用括号划分为不同的组。


【思路】

1. 这是一个题目用分治理法来解决。如果发现了一个操作符,则把这个表达式分为两部分,分别计算这两部分可能出现的所有结果,然后利用当前操作符计算所有可能出现的结果。

2. 为了节省计算,可以使用一个map把中间计算的结果保存下来。


【java代码1】

 1 public class Solution {
 2     public List<Integer> diffWaysToCompute(String input) {
 3         List<Integer> res = new LinkedList<>();
 4         for(int i = 0; i < input.length(); i++) {
 5             char ch = input.charAt(i);
 6             if(ch=='+' || ch=='-' || ch=='*') {
 7                 List<Integer> part1 = diffWaysToCompute(input.substring(0,i));
 8                 List<Integer> part2 = diffWaysToCompute(input.substring(i+1));
 9                 for(Integer p1 : part1) {
10                     for(Integer p2 : part2) {
11                         switch(ch) {
12                             case '+':
13                                 res.add(p1+p2); break;
14                             case '-':
15                                 res.add(p1-p2); break;
16                             case '*':
17                                 res.add(p1*p2); break;
18                         }
19                     }
20                 }
21             }
22         }
23         if(res.size() == 0) {
24             res.add(Integer.valueOf(input));
25         }
26         
27         return res;
28     }
29 }

【java代码2】

 1 public class Solution {
 2     public List<Integer> diffWaysToCompute(String input) {
 3         Map<String, List<Integer>> map = new HashMap<>();
 4         List<Integer> res = new LinkedList<>();
 5         for(int i = 0; i < input.length(); i++) {
 6             char ch = input.charAt(i);
 7             if(ch=='+' || ch=='-' || ch=='*') {
 8                 String s1 = input.substring(0,i);
 9                 String s2 = input.substring(i+1);
10                 List<Integer> part1 = map.getOrDefault(s1, diffWaysToCompute(s1));
11                 List<Integer> part2 = map.getOrDefault(s2, diffWaysToCompute(s2));
12                 for(Integer p1 : part1) {
13                     for(Integer p2 : part2) {
14                         switch(ch) {
15                             case '+':
16                                 res.add(p1+p2); break;
17                             case '-':
18                                 res.add(p1-p2); break;
19                             case '*':
20                                 res.add(p1*p2); break;
21                         }
22                     }
23                 }
24             }
25         }
26         if(res.size() == 0) {
27             res.add(Integer.valueOf(input));
28         }
29         
30         map.put(input, res);
31         return res;
32     }
33 }
原文地址:https://www.cnblogs.com/liujinhong/p/6480808.html