码迷,mamicode.com
首页 > 其他好文 > 详细

241.Different Ways to Add Parentheses

时间:2015-11-30 19:57:20      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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]

思路:递归。对字符串input,循环遍历,如果input[i]是运算符,将其切割为两个字符串,两个字符串的下标范围分别是0~i-1,i+1~n-1,递归求出这两个字符串所有组合计算的结果,置于result1和result2之中。然后对result1和result2之中的所有计算结果,一 一组合,求出对应之前切割的运算符对应的计算结果,计算结果写入result。有一种情况,如果刚开始传入的字符串就是一个数字,那么上述循环过程之中,result的大小并没有发生变化,此时,直接将这个字符串转换为数字,写入result即可。最后返回的result就是我们需要得到的所有计算结果。
  1. class Solution {
  2. public:
  3. vector<int> diffWaysToCompute(string input) {
  4. vector<int> result;
  5. for(int i=0;i<input.size();i++){
  6. char ch =input[i];
  7. if(ch==‘+‘||ch==‘-‘||ch==‘*‘){
  8. //递归对字符串0~i-1求计算的结果
  9. vector<int> result1=diffWaysToCompute(input.substr(0,i));
  10. //递归对字符串i+1~n-1求计算的结果
  11. vector<int> result2=diffWaysToCompute(input.substr(i+1));
  12. //对所有的结果,分别配对求与ch对应的计算结果,写入result之中
  13. for(auto x:result1){
  14. for(auto y:result2){
  15. if(ch==‘+‘)
  16. result.push_back(x+y);
  17. else if(ch==‘-‘)
  18. result.push_back(x-y);
  19. else
  20. result.push_back(x*y);
  21. }
  22. }
  23. }
  24. }
  25. //如果result.size()==0,标明子串中只有数字,将其转换为数字,写入result之中
  26. if(result.size()==0){
  27. result.push_back(atoi(input.c_str()));
  28. }
  29. //返回result
  30. return result;
  31. }
  32. };
改进:在这种求法中,有很多重复的子串被重复计算,因此,我们可以利用DP备忘录的方法,来以空间来换取效率。
改进程序如下:
  1. class Solution {
  2. private:
  3. unordered_map<string,vector<int>> map;
  4. public:
  5. vector<int> diffWaysToCompute(string input) {
  6. vector<int> result;
  7. for(int i=0;i<input.size();i++){
  8. char ch =input[i];
  9. if(ch==‘+‘||ch==‘-‘||ch==‘*‘){
  10. //递归对字符串0~i-1求计算的结果
  11. vector<int> result1= map.find(input.substr(0,i))==map.end()? diffWaysToCompute(input.substr(0,i)):map[input.substr(0,i)];
  12. //递归对字符串i+1~n-1求计算的结果
  13. vector<int> result2= map.find(input.substr(i+1))==map.end()? diffWaysToCompute(input.substr(i+1)) :map[input.substr(i+1)];
  14. //对所有的结果,分别配对求与ch对应的计算结果,写入result之中
  15. for(auto x:result1){
  16. for(auto y:result2){
  17. if(ch==‘+‘)
  18. result.push_back(x+y);
  19. else if(ch==‘-‘)
  20. result.push_back(x-y);
  21. else
  22. result.push_back(x*y);
  23. }
  24. }
  25. }
  26. }
  27. //如果result.size()==0,标明子串中只有数字,将其转换为数字,写入result之中
  28. if(result.size()==0){
  29. result.push_back(atoi(input.c_str()));
  30. }
  31. map[input]=result;
  32. //返回result
  33. return result;
  34. }
  35. };





241.Different Ways to Add Parentheses

标签:

原文地址:http://www.cnblogs.com/zhoudayang/p/5008038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!