different-ways-to-add-parentheses
2016-03-01 20:48
204 查看
https://leetcode.com/problems/different-ways-to-add-parentheses/
使用递归 枚举出所有的可能情况再相加相减或者相乘
使用递归 枚举出所有的可能情况再相加相减或者相乘
#include <vector> #include <string> using namespace std; class Solution { public: vector<int> diffWaysToCompute(string input) { vector<int> result; for (int i = 0; i < input.size(); i++) { if (input[i] == '+' || input[i] == '-' || input[i] == '*') { vector<int>left = diffWaysToCompute(input.substr(0, i)); vector<int>right = diffWaysToCompute(input.substr(i + 1)); for (int j = 0; j < left.size(); j++) for (int k = 0; k < right.size(); k++) { if (input[i] == '+') result.push_back(left[j] + right[k]); else if (input[i] == '-') result.push_back(left[j] - right[k]); else result.push_back(left[j] * right[k]); } } } if (result.empty()) result.push_back(atoi(input.c_str())); return result; } };
相关文章推荐
- CSS中cursor的pointer 与 hand-备
- codeforces 448C Painting Fence
- 计算几何 usaco Fencing the Cows 圈奶牛
- javascript中对象总结
- jQuery参考手册
- 结合firebug学习jQuery选择器
- jQuery--选择器详解
- CSS居中的方法总结
- js中智能右键菜单
- CSS布局模型思考
- 两个Jsp网页间登陆页简单后台验证,并且防止未登录用户绕过登陆页
- 移动前端开发之viewport的深入理解
- javascript同源策略
- ExtJS之Progressbar进度条的手动模式和自动模式。
- CheckBox全选,不要相信JQuery,还是原生的好~
- bootstrap3.3 兼容IE8处理方法
- ## 创建第一个node.js工程 ##
- #学习笔记#(47)AngularJS作用域$emit $bordercast
- javascript键值映射
- jQuery--jq对象和dom对象