LeetCode OJ : 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]
这里的主要思想是讲左右的子串分别算出来之后,再做全排列就行了,可能有很所重复计算所以说效率比较低
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> result;
int length = input.length();
for(int i = ; i < length; ++i){
char c = input[i];
if(c == '+' || c == '-' || c == '*'){
string inputLeft = input.substr(, i);
string inputRight = input.substr(i+);
vector<int>leftResult = diffWaysToCompute(inputLeft);
vector<int>rightResult = diffWaysToCompute(inputRight);
for(int j = ; j < leftResult.size(); ++j){
for(int k = ; k < rightResult.size(); ++k){
if(c == '+')
result.push_back(leftResult[j] + rightResult[k]);
else if(c == '-')
result.push_back(leftResult[j] - rightResult[k]);
else if(c == '*')
result.push_back(leftResult[j] * rightResult[k]);
}
}
}
}
if(result.empty())//这一步主要的作用是讲分治得到的最后的字符转换成数字
result.push_back(stoi(input));
return result;
}
};
java版本如下所示:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
ArrayList<Integer> ret = new ArrayList<Integer>();
for(int i = 0; i < input.length(); ++i){
char c = input.charAt(i);
if(c == '+' || c == '-' || c == '*'){
List<Integer> leftRet = diffWaysToCompute(input.substring(0, i));
List<Integer> rightRet = diffWaysToCompute(input.substring(i+1, input.length()));
for(int left : leftRet){
for(int right : rightRet){
if(c == '+'){
ret.add(left + right);
}else if(c == '-'){
ret.add(left - right);
}else{
ret.add(left * right);
}
}
}
}
}
if(ret.isEmpty()){
ret.add(Integer.parseInt(input));
}
return ret;
}
}
LeetCode OJ : Different Ways to Add Parentheses(在不同位置增加括号的方法)的更多相关文章
- 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 num ...
- [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】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 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 ...
随机推荐
- iOS 大批量弹幕小论(粒子弹幕)
一.现状 如今直播类.视频播放器等基本都有弹幕模式. 为了保持性能和内存可控,基本是在初始化的时候生成一个Pool(Pool的容量是设定好的), 也就是利用重用机制(可以想象一下UITableView ...
- platform_device和platform_driver的注册过程,及probe函数何时调用的分析 ⭐⭐⭐
add platform_device之后,需要注意的一个地方是这里,add是通过系统初始化里边调用platform_add_devices把所有放置在板级platform_device数组中的所有 ...
- POJ-2418 Hardwood Species(二叉搜索树)
思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++: 最后通过递归中序遍历输出结果. 思路很清晰,主要注意一下指针的使用,想一想为什么要这么用? 简单的解释就是,insert ...
- [Network Architecture]DPN(Dual Path Network)算法详解(转)
https://blog.csdn.net/u014380165/article/details/75676216 论文:Dual Path Networks 论文链接:https://arxiv.o ...
- 【cs231n】神经网络笔记笔记2
) # 对数据进行零中心化(重要) cov = np.dot(X.T, X) / X.shape[0] # 得到数据的协方差矩阵 数据协方差矩阵的第(i, j)个元素是数据第i个和第j个维度的协方差. ...
- jQuery实现输入框提示,当获取焦点时提示消失,当失去焦点时内容为空则显示提示,否则保留输入信息
首先看效果 默认状态下 获取焦点状态下 什么也没输入,离开 有输入离开 输入默认值离开 代码 <!DOCTYPE html> <html> <head> <m ...
- jQuery基本筛选选择器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 自学Java测试代码 - 简单地Student类
2017-08-23 23:45:38 writer:pprp 写这个还蛮开心的 package test; public class Student { //创建成员变量 String name ...
- HMM代码实践
本文主要转载于:http://www.52nlp.cn/hmm-learn-best-practices-eight-summary 这个文章是边看边实践加上自己的一些想法生成的初稿..... 状态转 ...
- 返回值过长时被nginx截断的解决办法
今天在写接口时碰到了这个问题,返回json格式的数据,但是被截断了经过排查,才发现是数据过大超出缓冲区最大容量,而将数据写入临时文件时又没有权限,所以再返回时,超出缓冲区的数据将丢失解决方法:给fas ...