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(在不同位置增加括号的方法)的更多相关文章

  1. 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 ...

  2. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  3. (medium)LeetCode 241.Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. 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 ...

  5. [LeetCode#241]Different Ways to Add Parentheses

    Problem: Given a string of numbers and operators, return all possible results from computing all the ...

  6. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  7. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  8. 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]* ...

  9. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

随机推荐

  1. Hive练习

    一.基础DDL练习 SHOW DATABASES; CREATE DATABASE IF NOT EXISTS db1 COMMENT 'Our database db1'; SHOW DATABAS ...

  2. java: -source 1.6 中不支持 switch 中存在字符串

    最近在使用IDEA进行单个文件编译的时候给我报错,如题. 解决办法:将 Modules --->Sources ---> Language level 改为 7.0就ok了.

  3. python---自动群发邮件

    生活中我们经常发送邮件,那么我们能不能用Python写一个自动发送邮件的功能呢?答案是肯定的!!! 开始实现功能之前我们需要开启我们邮箱的 IMAP/SMTP功能,我们先了解一下什么是IMAP/SMT ...

  4. 在linux上使用tomcat服务器图片验证码不显示问题

    背景描述:在liunx系统上,使用tomcat中间件,访问web项目,登录页面的图片验证码显示不出来,但是在window系统上可以正常显示 解决方案:设置一下这个文件tomcat/bin/catali ...

  5. jackson序列化字段字母大小写及字段名重复

    一:Jackson默认的属性发现规则将会查找到如下所述的属性: 1.所有被public修饰的字段(成员变量): 2.所有被public修饰的getter(即形如“getXxx()”的方法): 3.所有 ...

  6. 百度云如何免费扩容至2055G?

    百度云如何免费扩容至2055G? 上篇说到整一个新的百度账号,那么5G的百度云内存肯定满足不了我们收集癖的需求.那么就来了解一下怎么扩容吧. 主要是在手机端实现的 用这个新的百度账号在手机APP上登录 ...

  7. 二进制转化 - bitset

    2017-08-28 10:55:17 writer:pprp 在之前写了一个关于bitset用法的贴之后,这是第一次运用,不得不说如果不用的话还是一头雾水 以后写代码要标记开始时间,和结束时间了,我 ...

  8. TCP状态切换流程

    enum { /* * Description of States: * * TCP_SYN_SENT sent a connection request, waiting for ack * * T ...

  9. 02_zookeeper集群安装

    zookeeper集群安装 (1)   下载zookeeper安装包,并上传到要组成zookeeper集群的多个机器上 我放置的目录:/usr/local/src/zookeeper-3.4.5.ta ...

  10. 好的Mysql 查询语句

    select swr.id,swr.name,swr.sort as type,count(swl.id) as nums,ifnull(sum(swl.package_num),0) package ...