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 ...
随机推荐
- NodeJS NPM HTTPS
npm config set registry http://registry.npmjs.org/
- 20145321 《Java程序设计》第7周学习总结
20145321 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 13.1 认识时间与日期 1.格林威治时间(GMT) 观察太阳得来 2.世界时(UT) 3.国 ...
- mfc配置CAN通信
配置:把kerneldlls文件夹.ControlCAN.dll.ControlCAN.lib放在工程下面(debug和Release下面,最后需要exe和这些文件在一起):右键工程属性,链接器-&g ...
- 再也不学AJAX了!(三)跨域获取资源 ① - 同源策略
我们之前提到过,AJAX技术使开发者能够专注于互联网中数据的传输,而不再拘泥于数据传输的载体.通过AJAX技术,我们获取数据的方式变得更加灵活,可控和优雅. 但是AJAX技术并不是一把万能钥匙,互联网 ...
- @component的注解
1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...
- Centos6优化系统服务脚本
#!/bin/bash SysVer=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk -F'[ .]+' '{print $ ...
- mysql系列之多实例介绍
介绍: mysql多实例,简单理解就是在一台服务器上,mysql服务开启多个不同的端口(如3306.3307),运行多个服务进程.这些 mysql 服务进程通过不同的 socket来监听不同的数据端口 ...
- Linux 网站文件和数据库全量备份 一键脚本(支持FTP,Google Drive)
原文连接: https://teddysun.com/469.html 此文为转载,建议查看秋水大神的原文,排版更容易查看,另外,建议查看脚本源码,方便了解脚本运行过程, 脚本已测试,大神的脚本一如既 ...
- TSP - 状态压缩dp
2017-08-11 21:10:21 艾教写的 #include<iostream> #include<cstdio> #include<cstring> #in ...
- mac iterm2 打开Linux 服务器文件乱码
我的mac 上用是iterm2终端, Shell 环境是zsh.ssh 到Linux 服务器上查看一些文件时,中文乱码. 这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8字符集 ...