Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee82ae5

Browse files
Add solution for Different Ways to Add Parentheses
1 parent 3587ce1 commit ee82ae5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
8787
- Subtree Of Another Tree | [Problem](https://leetcode.com/problems/subtree-of-another-tree) | [Java Solution](src/javacode/solutions/SubtreeOfAnotherTree.java)
8888
- Climbing Stairs | [Problem](https://leetcode.com/problems/climbing-stairs) | [Java Solution](src/javacode/solutions/ClimbingStairs.java)
8989
- All Possible Full Binary Trees | [Problem](https://leetcode.com/problems/all-possible-full-binary-trees) | [Java Solution](src/javacode/solutions/AllPossibleFullBinaryTrees.java)
90+
- Different Ways to Add Parentheses | [Problem](https://leetcode.com/problems/different-ways-to-add-parentheses) | [Java Solution](src/javacode/solutions/DifferentWaysToAddParentheses.java)
9091

9192
### BFS
9293
- Binary Tree Level Inorder Traversal | [Problem](https://leetcode.com/problems/binary-tree-level-order-traversal) | [Java Solution](src/javacode/solutions/BinaryTreeLevelOrderTraversal.java)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package javacode.solutions;
2+
3+
import java.util.*;
4+
5+
// [Problem] https://leetcode.com/problems/different-ways-to-add-parentheses
6+
class DifferentWaysToAddParentheses {
7+
// Recursion
8+
public List<Integer> diffWaysToCompute(String input) {
9+
List<Integer> permutations = new ArrayList<>();
10+
for (int i = 0; i < input.length(); i++) {
11+
char c = input.charAt(i);
12+
if (c == '-' || c == '+' || c == '*') {
13+
String left = input.substring(0, i);
14+
String right = input.substring(i + 1);
15+
List<Integer> leftPermutations = diffWaysToCompute(left);
16+
List<Integer> rightPermutations = diffWaysToCompute(right);
17+
for (int leftPermutation : leftPermutations) {
18+
for (int rightPermutation : rightPermutations) {
19+
int permutation = compute(leftPermutation, rightPermutation, c);
20+
permutations.add(permutation);
21+
}
22+
}
23+
}
24+
}
25+
if (permutations.size() == 0) {
26+
permutations.add(Integer.valueOf(input));
27+
}
28+
return permutations;
29+
}
30+
31+
private int compute(int num1, int num2, char operator) {
32+
if (operator == '*') {
33+
return num1 * num2;
34+
} else if (operator == '+') {
35+
return num1 + num2;
36+
} else {
37+
return num1 - num2;
38+
}
39+
}
40+
41+
// Test
42+
public static void main(String[] args) {
43+
DifferentWaysToAddParentheses solution = new DifferentWaysToAddParentheses();
44+
45+
String input = "2*3-4*5";
46+
List<Integer> output = solution.diffWaysToCompute(input);
47+
48+
// expected: [-34, -14, -10, -10, 10] in any order
49+
System.out.println("actualOutput " + output);
50+
}
51+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /