leetCode刷题归纳-Divide and Conquer(241. Different Ways to Add Parentheses)
来源:互联网 发布:网络借贷资金存管 编辑:程序博客网 时间:2024/06/05 15:23
Divide的题目相比起来难度会比较大,主要是分治的思想比较难以与实际应用结合起来,下面是题目描述:
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]
最初的考虑
主要就是对标点符号的作用域要进行Divide,然后对于划分的每一个区域进行Conquer(计算),对输入的string进行扫描,观察到只有当扫描到运算符的时候才需要进行处理,对预算符的左右两部分进行分治运算,以题目中的例子为例画出处理的流程图:
需要注意两个方面的问题:
- 跳出递归的条件设置:当被分治的对象为不包含运算符的字符串时,将字符串转化为int类型跳出,代码表现为:
if (result.empty()) result.push_back(atoi(input.c_str()));
- 被分治的左右两部分(merge1 +/-/* merge2)可能存在多个可能值,利用循环计算出所有可能的值,表现在代码上:
for (auto n1 : result1) { for (auto n2 : result2) { if (cur == '+') result.push_back(n1 + n2); else if (cur == '-') result.push_back(n1 - n2); else result.push_back(n1 * n2); }
将以上的思想归纳为代码如下:
class Solution {public: vector<int> diffWaysToCompute(string input) { 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 recursively vector<int> result1 = diffWaysToCompute(input.substr(0, i)); vector<int> result2 = diffWaysToCompute(input.substr(i+1)); for (auto n1 : result1) { for (auto n2 : result2) { if (cur == '+') result.push_back(n1 + n2); else if (cur == '-') result.push_back(n1 - n2); else result.push_back(n1 * n2); } } } } // if the input string contains only number if (result.empty()) result.push_back(atoi(input.c_str())); return result; }};
算法改进
以上算法有一个比较大的问题,就是进行了很多重复的运算,以输入为“2*3-4*5”为例,如果按照上面的思路,如下图所示,可以发现4*5运算做了两次,在输入的字符串规模很大的情况下,将浪费很多计算时间:
改进的想法是利用效率很高的unorder_map将之前计算过的字符串存储起来(典型的空间换时间的思路),实际上对于代码而言改动并不大:
class Solution {public: vector<int> diffWaysToCompute(string input) { unordered_map<string,vector<int>>dpMap; return DpCompute(input,dpMap); } vector<int> DpCompute(string s,unordered_map<string,vector<int>>&dpMap){ vector<int>res; //主要的改动体现在这里,如果 if (dpMap.find(s)!=dpMap.end()) return dpMap[s]; for(int i=0;i<s.size();i++){ char cur=s[i]; if (cur == '+' || cur == '-' || cur == '*'){ vector<int>res1=DpCompute(s.substr(0,i),dpMap); vector<int>res2=DpCompute(s.substr(i+1),dpMap); for(int x:res1){ for(int y:res2){ if (cur == '+' ) res.push_back(x+y); else if (cur == '-' ) res.push_back(x-y); else res.push_back(x*y); } } } } if(res.empty()) res.push_back(stoi(s)); //将每一次计算过的string可能的值映射到数组中vector<int> dpMap[s]=res; return res; }};
0 0
- leetCode刷题归纳-Divide and Conquer(241. Different Ways to Add Parentheses)
- Divide and Conquer -- Leetcode problem241:Different Ways to Add Parentheses
- [Divide and Conquer]241. Different Ways to Add Parentheses
- 241. Different Ways to Add Parentheses(divide and conquer)
- Divide and Conquer:241. Different Ways to Add Parentheses
- 【算法】【Divide and conquer】Different Ways to Add Parentheses
- Leetcode divide & conquer || Different Ways to Add Parentheses
- leetcode_c++:Divide and Conquer:Different Ways to Add Parentheses(241)
- Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses
- [leetcode] 241.Different Ways to Add Parentheses
- Leetcode-241.Different Ways to Add Parentheses
- 241. Different Ways to Add Parentheses LeetCode
- Leetcode- 241.Different Ways to Add Parentheses
- [LeetCode]241. Different Ways to Add Parentheses
- [leetcode] 241. Different Ways to Add Parentheses
- LeetCode *** 241. Different Ways to Add Parentheses
- LeetCode 241. Different Ways to Add Parentheses
- LeetCode 241. Different Ways to Add Parentheses
- 4Sum II
- 线程死锁
- vue-resource post数据时碰到Django csrf
- Oracle plsql 查询时中文乱码
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- leetCode刷题归纳-Divide and Conquer(241. Different Ways to Add Parentheses)
- JVM学习之类加载
- 欢迎使用CSDN-markdown编辑器
- 525. Contiguous Array Medium
- MyBatis 配置中那些值得注意的坑
- Android自定义View 一附<点,线,矩形,扇形和文字的简单用法>
- 泛型(二)->擦除&擦除带来的问题
- ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock
- python 按普通字典排序(小写字母,排在大写字母前)