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. NC二次开发常用的方法

    //这张表存放的是所有单据模板的信息表 如果不知道单据模板的信息后可在数据库中查询PUB_BILLTEMPLET//这张表是打印模板的表改模板可以再此表修改pub_print_template//获取 ...

  2. Datatable To List<Entity>

    public static DataTable ToDataTable<T>(this IEnumerable<T> varlist) { DataTable dtReturn ...

  3. ONVIF学习-ONVIF开发框架搭建(C++)

    第一步.下载gsoap 从gsoap官网(http://www.genivia.com/products.html#notice)下载最新版gsoap(博主用的是gsoap_2.8.45).gsoap ...

  4. centos6.9 升级glibc(升级到 2.17版)

    原系统centos6.9自带GLIBC_2.12,安装一些软体提示版本不对,决定升级. wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz tar ...

  5. 20135302魏静静——linux课程第三周实验及总结

    linux课程第三周实验及总结 一.实验:跟踪分析Linux内核的启动过程 使用gdb跟踪调试内核从start_kernel到init进程启动 使用实验楼的虚拟机打开shell cd LinuxKer ...

  6. shell编程(一)

    转义和引用 引入问题:之前我们知道了变量名前面加上$符号代表引用变量,但是如果我现在就需要打印出$符号该怎么办呢?想想我们在python中怎么做的,答案是转义. 转义 Shell中有两种字符一种是普通 ...

  7. Android 7.1 快捷方式 Shortcuts

    转载请注明出处:王亟亟的大牛之路 前些天就看到相关内容了,但是最近吸毒比较深(wow),所以没有紧跟潮流,今天补一篇. 先安利:https://github.com/ddwhan0123/Useful ...

  8. CentOS 6.2配置本地yum源

    转载自http://www.cnblogs.com/centoser/articles/2411694.html#undefined 一.挂载本地光盘到系统:把Cent6.2安装光盘放入光驱,在终端命 ...

  9. jmeter-对响应数据进行unicode转码

    1,请求接口成功后,返回数据为unicode编码,查看不方便

  10. Kubernetes服务目录的设计

    [编者的话]OpenShift 3.6新版本包括新的服务目录和服务中介技术预演版.它们是基于Kubernetes的孵化项目Kubernetes Service Catalog project.服务目录 ...