标签:
unordered_map<string, vector<int> > book; class Solution { public: vector<int> diffWaysToCompute(string input) { if(book.count(input)) return book[input]; vector<int> outputs; int n = input.length(); for (int i = 0; i < n; i++) { if (input[i] == ‘+‘ || input[i] == ‘-‘ || input[i] == ‘*‘) { string left = input.substr(0, i); string right = input.substr(i + 1, n - i - 1); vector<int> lval = diffWaysToCompute(left); vector<int> rval = diffWaysToCompute(right); for (int l : lval) { for (int r : rval) { switch (input[i]) { case ‘+‘: outputs.push_back(l + r); break; case ‘-‘: outputs.push_back(l - r); break; default: outputs.push_back(l * r); } } } } } if (outputs.empty()) outputs.push_back(stoi(input)); book[input] = outputs; return outputs; } };
【LeetCode】241. Different Ways to Add Parentheses
标签:
原文地址:http://www.cnblogs.com/mmyz-sysu-panjn/p/LeetCode241.html