LN : leetcode 241 Different Ways to Add Parentheses
lc 241 Different Ways to Add Parentheses
241 Different Ways to Add Parentheses
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]
分治法 Accepted##
这几周的算法课,老师一直在讲分治法的应用,所以现在尝试利用分治法去解决这个问题。利用递归,将复杂的问题拆分成同样的小的问题。
分治法的精髓:
分--将问题分解为规模更小的子问题;
治--将这些规模更小的子问题逐个击破;
合--将已解决的子问题合并,最终得出“母”问题的解;
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> ans;
for (int i = 0; i < input.size(); i++) {
char c = input[i];
if (ispunct(c)) {
for (auto a : diffWaysToCompute(input.substr(0, i))) {
for (auto b : diffWaysToCompute(input.substr(i+1))) {
ans.push_back(c == '-' ? a-b : c == '+' ? a+b : a*b);
}
}
}
}
return ans.size() ? ans : vector<int>{stoi(input)};
}
};
LN : leetcode 241 Different Ways to Add Parentheses的更多相关文章
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 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
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
随机推荐
- HTML学习之Flex 布局
一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局. .box ...
- java生成xml
import java.io.File;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.ut ...
- 实战恢复2950交换机的IOS
本来想用两台交换机做实验的,可是通过console口进入其中一台交换机后却发现这个台交换器的IOS文件丢失了 本来正常进入交换机后应该是首先进入到用户模式的,而且提示符应该是">&qu ...
- ubuntu10.04 建V
ubuntu10.04架设vpn服 vpn 安装: pptpd:apt-get install pptpd 1. 配置网络IP地址,编辑 vim /etc/pptpd.conf ,去掉下面两行前面# ...
- System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。
错误背景: 操作系统:编程环境:VS2013. 语言:VB.net: 数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...
- eclipse中j2ee(struts2)部署及相关问题释疑
1.eclipse中进行web项目开发时.部署的时候和利用myeclipse部署时有非常大不同,由于在myeclipse的工具栏中有一个部署button.而且在myeclipse的preference ...
- javascript的继承方法
一.构造函数继承 该方法最简单,使用call或者apply方法,将父对象的构造函数绑定到子对象上. function Parent(name){ this.name = name; this.colo ...
- 浏览器判断是否安装了ios/android客户端程序
最近在做一个项目,该项目的前身是为mobile browser量身打造的一个网站.现在有这样一个需求: 当用户在用mobile browser浏览该网站的时候会点击一个按钮/超链接,通过这个按钮的点击 ...
- B. Error Correct System (CF Round #296 (Div. 2))
B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- docker 默认用户和密码
windows 下安装docker依赖boot2docker镜像默认用户和密码 标签: dockerroot密码 2017-03-26 14:13 2796人阅读 评论(0) 收藏 举报 分类: 虚 ...