LeetCode----241. Different Ways to Add Parenthese(M)分治
1 题目
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]
2 分析和实现
用分治思想实现
class Solution {
public:vector<int> diffWaysToCompute(string input) {vector<int> res;for(int i = 0; i < input.size(); i++) {char cur = input[i];if(cur == '+' || cur == '-' || cur == '*') {vector<int> res_left = diffWaysToCompute(input.substr(0, i));vector<int> res_right = diffWaysToCompute(input.substr(i + 1));for(int x = 0; x < res_left.size(); x++) {for(int y = 0; y < res_right.size(); y++) {if(cur == '+')res.push_back(res_left[x] + res_right[y]);else if(cur == '-')res.push_back(res_left[x] - res_right[y]);else if(cur == '*')res.push_back(res_left[x] * res_right[y]);}}}}//单独一个数字时if(res.empty()) {res.push_back(atoi(input.c_str()));}return res; }
};
LeetCode----241. Different Ways to Add Parenthese(M)分治相关推荐
- [LeetCode]241.Different Ways to Add Parenthese
description Given a string of numbers and operators, return all possible results from computing all ...
- leetcode 241. Different Ways to Add Parentheses | 241. 为运算表达式设计优先级(Java)
题目 https://leetcode.com/problems/different-ways-to-add-parentheses/ 题解 参考:C++ Solution [Faster than ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 【LeetCode】241. Different Ways to Add Parentheses
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- LeedCode 241. Different Ways to Add Parentheses
一.题目 Given a string expression of numbers and operators, return all possible results from computing ...
- LeetCode-241. Different Ways to Add Parentheses [C++][Java]
LeetCode-241. Different Ways to Add Parentheseshttps://leetcode.com/problems/different-ways-to-add-p ...
- 【重点!DP】LeetCode 639. Decode Ways II
LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...
- 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways
LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...
- LeetCode Different Ways to Add Parentheses(递归,动态规划)
问题:给出一个表达式,求其添加括号后可能的结果 思路:第一种方式是根据运算符号作分割,分为两个子问题. 第二种方式是在第一种方式下作记忆,避免重复计算问题. 第二种方式使用动态规划,先将操作数和操作符 ...
最新文章
- 平安京服务器维护不能打字,《决战!平安京》:玩的真的累,我真的是服了这破游戏的举报系统...
- 多个Google账户合并(共享)数据
- 4/100. Maximum Depth of Binary Tree
- memcpy(cv::Mat.data,unsigned char *,320x240),保存数据失真问题解决方法
- 【洛谷3157】[CQOI2011] 动态逆序对(CDQ分治)
- tiledmap 图块属性_TiledMap地图使用
- 铃声截取软件android6,铃声剪辑
- 标准误计算机excel公式,关于excel计算标准差SD和标准误SE的方法
- 汽车称重软件系统配置(一)
- R语言---ggplot图片边缘空白设置和ggplot映射的shape值多余6个
- Matlab中的mod()函数
- 3d打印打印头heating failure报错
- 小雷的冰茶几 3386
- Writeback和Writethrough区别
- Ubuntu18.04运行校园网客户端
- SolidWorks二次开发-关于录制宏保存的格式
- Greenplum集群部署和架构优化,我总结了5000字的心得
- Arduino远程无线通信
- 数据探索与数据预处理的实验报告
- Qgis 加载在线地图:如高德、天地图、OSM等