public class Solution {
public List diffWaysToCompute(String input) {
List ret = new LinkedList();
for (int i=0; i<input.length(); i++) {
if (input.charAt(i) == '-' ||
input.charAt(i) == '' ||
input.charAt(i) == '+' ) {
String part1 = input.substring(0, i);
String part2 = input.substring(i+1);
List part1Ret = diffWaysToCompute(part1);
List part2Ret = diffWaysToCompute(part2);
for (Integer p1 : part1Ret) {
for (Integer p2 : part2Ret) {
int c = 0;
switch (input.charAt(i)) {
case '+': c = p1+p2;
break;
case '-': c = p1-p2;
break;
case '': c = p1*p2;
break;
}
ret.add(c);
}
}
}
}
if (ret.size() == 0) {
ret.add(Integer.valueOf(input));
}
return ret;
}
}
convert to c++
Assistant
Certainly! Below is the C++ equivalent of the provided Java Solution class. This C++ version uses std::vector<int> to store the results and follows the same recursive approach to compute all possible results from different ways of grouping the input expression.
cpp
#include <vector>
#include <string>
#include <cctype>
using namespace std;
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> ret;
for (int i = 0; i < input.length(); ++i) {
char current = input[i];
// Check if the current character is an operator
if (current == '+' || current == '-' || current == '*') {
// Split the input into two parts around the operator
string part1 = input.substr(0, i);
string part2 = input.substr(i + 1);
// Recursively compute the results for both parts
vector<int> part1Results = diffWaysToCompute(part1);
vector<int> part2Results = diffWaysToCompute(part2);
// Combine the results from both parts using the current operator
for (int num1 : part1Results) {
for (int num2 : part2Results) {
int computedValue = 0;
switch (current) {
case '+':
computedValue = num1 + num2;
break;
case '-':
computedValue = num1 - num2;
break;
case '*':
computedValue = num1 * num2;
break;
}
ret.push_back(computedValue);
}
}
}
}
// If no operators were found, the input is a number. Convert and add it to the result.
if (ret.empty()) {
ret.push_back(stoi(input));
}
return ret;
}
};
Explanation:
Function Signature:
The function diffWaysToCompute takes a string input and returns a vector<int> containing all possible computation results.
Iterating Through the Input:
The function iterates through each character of the input string to check for operators (+, -, *).
Splitting the Input:
When an operator is found at position i, the input string is split into two parts:
part1: Substring from the start to the character before the operator.
part2: Substring from the character after the operator to the end.
Recursive Calls:
The function recursively computes all possible results for part1 and part2.
Combining Results:
For each result from part1 and each result from part2, the function applies the current operator to compute a new result, which is then added to the ret vector.
Base Case:
If no operators are found in the input string, it means the input is a single number. The function converts the string to an integer using stoi and adds it to the ret vector.
Returning the Result:
Finally, the function returns the ret vector containing all possible computation results.