本文主要是介绍Leetcode——241. Different Ways to Add Parentheses,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
description
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]
solution
这一题挺难的,反正思想就是递归求解。递归可以把前后两部分从i位置处分开,然后依次计算前一部分和后一部分的各种可能的解。
递归的设计不太好想!
class Solution {
public:vector<int> diffWaysToCompute(string input) {if(input.size()==0) return {};vector<int>res;for(int i=0;i<input.size();i++){if(input[i]!='*'&&input[i]!='+'&&input[i]!='-') continue;vector<int> tem1=diffWaysToCompute(input.substr(0,i));auto tem2=diffWaysToCompute(input.substr(i+1));for(auto vec1:tem1)for(auto vec2:tem2){switch(input[i]){case '+':res.push_back(vec1+vec2);break;case '-':res.push_back(vec1-vec2);break;case '*':res.push_back(vec1*vec2);break;default:break;}}}if(res.size()==0)return vector<int>{stoi(input)};elsereturn res;}
};
自己写的不对的代码:
每次求取两个数的运算,把n个数的运算转化为n-1个,依次递减到2个数的运算。
但是存在重复的问题:
写了半天,也粘贴出来:
class Num241 {//递归求解
public:vector<int> diffWaysToCompute(string input) {vector<int> res;vector<int> num;vector<char>ope;int i = 0;while (i<input.length()){int a = 0;int sign = 1;while (i<input.length() && (isdigit(input[i]) || (input[i] == '-' && (!isdigit(input[i + 1])))))//类似2-1这种{if (input[i] == '-') sign = -1;elsea = a * 10 + (input[i] - '0');i++;}num.push_back(a);if (i<input.length())ope.push_back(input[i]);i++;}int count = num.size();helper(res, num, ope);return res;//unordered_map<int,int> resres;//unordered_map<int, int>::iterator it;//for (int i = 0;i < res.size();i++)//{// resres[res[i]] = 1;//}//vector<int> newres;//for (it = resres.begin();it != resres.end();it++)// newres.push_back(it->first);//return newres;}
private:void helper(vector<int>& res, vector<int> num, vector<char>ope){if (num.size() == 2){int a = num[0], b = num[1];cout <<"res"<< a << " " << b <<" "<< ope[0]<<endl;switch (ope[0]){case '+':res.push_back(a + b);break;case '-':res.push_back(a - b);break;case '*':res.push_back(a*b);break;default:break;}//return;}else{for (int i = 0; i<num.size() - 1; i++)//{int temp = 0;int a = num[i], b = num[i + 1];cout << a << " " << b << endl;switch (ope[i]){case '+':temp = a + b;break;case '-':temp = a - b;break;case '*':temp = a*b;break;default:break;}vector<int> num_copy(num);vector<char> ope_copy(ope);num_copy[i] = temp;num_copy.erase(num_copy.begin() + i + 1);ope_copy.erase(ope_copy.begin() + i);helper(res, num_copy, ope_copy);}}}
};
另外一种递归方法(去重复):
class Solution {
public:vector<int> diffWaysToCompute(string input) {unordered_map<string, vector<int>> dpMap;return computeWithDP(input, dpMap);}vector<int> computeWithDP(string input, unordered_map<string, vector<int>> &dpMap) {vector<int> result;int size = input.size();for (int i = 0; i < size; i++) {char cur = input[i];if (cur == '+' || cur == '-' || cur == '*') {// Split input string into two parts and solve them recursivelyvector<int> result1, result2;string substr = input.substr(0, i);// check if dpMap has the result for substrif (dpMap.find(substr) != dpMap.end())result1 = dpMap[substr];//没有重复的elseresult1 = computeWithDP(substr, dpMap);//有重复的,继续递归下去substr = input.substr(i + 1);if (dpMap.find(substr) != dpMap.end())result2 = dpMap[substr];elseresult2 = computeWithDP(substr, dpMap);for (auto n1 : result1) {for (auto n2 : result2) {if (cur == '+')result.push_back(n1 + n2);else if (cur == '-')result.push_back(n1 - n2);elseresult.push_back(n1 * n2);}}}}// if the input string contains only numberif (result.empty())result.push_back(atoi(input.c_str()));// save to dpMapdpMap[input] = result;return result;}
};
这篇关于Leetcode——241. Different Ways to Add Parentheses的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!