分治法學習之多項式運算順序
阿新 • • 發佈:2019-01-02
LeetCode題目 241Different 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]
分析
這道題可以用分治法來做。分治法有三個步驟。1、如何分成n個子問題;2、遞迴求解,定義原子問題;3、如何合併子問題。再看這道題,顯而易見地看到輸入的字串包括k個數和k-1個運算子。然後要求我們根據不同的運算順序得到不同的運算結果。所以第一個步驟我們確定了子問題就是將k-1個運算子拆成子問題。第二個步驟,遞迴求解。遞迴的思路是將運算子作為中間的斷開點,左右兩邊分別遞迴下去。原子問題是當沒有運算子的時候, 直接返回該數。第三個步驟就是將左右兩邊得到的陣列相乘,得到的新陣列就是所有可能的結果。
預先處理資料
在本題中,我不是直接用string作為遞迴的引數,而是先將string處理成一個整型陣列和一個運算子的char陣列。然後在將這兩個陣列分開作為子問題。
vector<int> diffWaysToCompute(string input) {
vector<int> nums;
vector<char> punc;
string str;
int num;
for (int i = 0; i < input.length(); i++) {
if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
punc.push_back(input[i]);
num = StrToInt(str);
nums.push_back(num);
num = 0;
str = "";
}
else {
str += input[i];
}
}
num = StrToInt(str);
nums.push_back(num);
vector<int> result = ComputeVec(nums, punc);
return result;
}
遞歸併確定原子問題
vector<int> ComputeVec(vector<int> nums, vector<char> punc) {
vector<int> result;
if (nums.size() != punc.size() + 1) return result;
if (punc.size() == 0) result.push_back(nums[0]);
else {
for (int i = 0; i < punc.size(); i++) {
vector<int> left = truncInt(nums, 0, i);
vector<int> right = truncInt(nums, i + 1, nums.size() - 1);
vector<char> leftpunc = truncChar(punc, 0, i - 1);
vector<char> rightPunc = truncChar(punc, i + 1, punc.size()-1);
vector<int> vec = ComputeTwoVec(ComputeVec(left, leftpunc), ComputeVec(right, rightPunc), punc[i]);
for (int j = 0; j < vec.size(); j++) result.push_back(vec[j]);
}
}
return result;
}
合併遞迴結果
vector<int> ComputeTwoVec(vector<int> left, vector<int> right, char c) {
vector<int> result;
for (int i = 0; i < left.size(); i++) {
for (int j = 0; j < right.size(); j++) {
result.push_back(Compute(left[i], right[j], c));
}
}
return result;
}
完整程式碼
using namespace std;
class Solution {
public:
int StrToInt(string str) {
int len = str.length();
int i = 0, result = 0;
while (i < len) {
result = result * 10 + (str[i] - '0');
i++;
}
return result;
}
int Compute(int a, int b, char c) {
if (c == '+') return a + b;
if (c == '-') return a - b;
if (c == '*') return a*b;
return 0;
}
vector<int> truncInt(vector<int> vec, int begin, int end) {
vector<int> result;
if (end < begin || end >= vec.size()) return result;
for (int i = begin; i <= end; i++) {
result.push_back(vec[i]);
}
return result;
}
vector<char> truncChar(vector<char> vec, int begin, int end) {
vector<char> result;
if (end < begin || end >= vec.size()) return result;
for (int i = begin; i <= end; i++) {
result.push_back(vec[i]);
}
return result;
}
vector<int> ComputeTwoVec(vector<int> left, vector<int> right, char c) {
vector<int> result;
for (int i = 0; i < left.size(); i++) {
for (int j = 0; j < right.size(); j++) {
result.push_back(Compute(left[i], right[j], c));
}
}
return result;
}
vector<int> ComputeVec(vector<int> nums, vector<char> punc) {
vector<int> result;
if (nums.size() != punc.size() + 1) return result;
if (punc.size() == 0) result.push_back(nums[0]);
else {
for (int i = 0; i < punc.size(); i++) {
vector<int> left = truncInt(nums, 0, i);
vector<int> right = truncInt(nums, i + 1, nums.size() - 1);
vector<char> leftpunc = truncChar(punc, 0, i - 1);
vector<char> rightPunc = truncChar(punc, i + 1, punc.size()-1);
vector<int> vec = ComputeTwoVec(ComputeVec(left, leftpunc), ComputeVec(right, rightPunc), punc[i]);
for (int j = 0; j < vec.size(); j++) result.push_back(vec[j]);
}
}
return result;
}
vector<int> diffWaysToCompute(string input) {
vector<int> nums;
vector<char> punc;
string str;
int num;
for (int i = 0; i < input.length(); i++) {
if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
punc.push_back(input[i]);
num = StrToInt(str);
nums.push_back(num);
num = 0;
str = "";
}
else {
str += input[i];
}
}
num = StrToInt(str);
nums.push_back(num);
vector<int> result = ComputeVec(nums, punc);
return result;
}
};
參考答案
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> res;
for (int i=0; i<input.size();i++) {
if (!isdigit(input[i])) {
string front = input.substr(0, i);
string back = input.substr(i+1);
vector<int> frontR = diffWaysToCompute(front);
vector<int> backR = diffWaysToCompute(back);
for (int j=0; j<frontR.size(); j++) {
for (int k=0; k<backR.size(); k++) {
if (input[i]=='+') res.push_back(frontR[j]+backR[k]);
if (input[i]=='-') res.push_back(frontR[j]-backR[k]);
if (input[i]=='*') res.push_back(frontR[j]*backR[k]);
}
}
}
}
if (res.size()==0) res.push_back(atoi(input.c_str()));
return res;
}
};
思考
這道題看了參考答案之後,感覺思路相差不多,但是自己的步驟多了幾倍。主要差別是,本題可以直接用string作為引數傳遞進去而並非轉化成vector,並且vector沒有很明顯的根據位置擷取資料,而是要自定義的一個函式來擷取。此外,vector又規定了內容型別,這樣對程式碼的可讀性和空間複雜度影響很大。另外,stdlib.h庫裡面有一個直接將string轉化為int的函式atoi,自己定義增加了程式碼量。