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(object):
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
A: (2*3)-4*5
((2*3)-4)*5=>1
(2*3)-(4*5)=>2
B:
2*(3-4)*5
(2*(3-4))*5=>1
2*((3-4)*5)=>2
"""
import re
tokens = re.split('(\D)', input)
nums = map(int, tokens[::2])
ops = map({'+': operator.add, '-': operator.sub, '*': operator.mul}.get, tokens[1::2])
def build(lo, hi):
if lo == hi:
return [nums[lo]]
ans = []
for i in range(lo, hi):
for a in build(lo, i):
for b in build(i+1, hi):
ans.append(ops[i](a, b))
return ans
return build(0, len(ops))

re.split("(\D)", "2+3-1")
['2', '+', '3', '-', '1']

241. Different Ways to Add Parentheses——本质:DFS的更多相关文章

  1. 241. Different Ways to Add Parentheses

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

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

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

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

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

  5. LC 241. Different Ways to Add Parentheses

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

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

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

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

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

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

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

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

随机推荐

  1. HNOI2006-公路修建问题(二分答案+并查集)

    公路修建问题 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织成立了,旨 ...

  2. Python标准库之Sys模块使用详解

    sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. 处理命令行参数 在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称. 使用sy ...

  3. MongoDB入门教程之C#驱动操作实例

    实体类: using MongoDB.Bson; namespace WindowsFormsApp { class User { //public ObjectId _id; //BsonType. ...

  4. NYOJ 128 前缀式计算

    前缀式计算 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 先说明一下什么是中缀式: 如2+(3+4)*5这种我们最常见的式子就是中缀式. 而把中缀式按运算顺序加上括 ...

  5. JavaScript基础知识之——Location 对象详解

    属性 描述 location.hash 设置或取得 URL 中的锚 location.host 设置或取得 URL 中主机(包括端口号) location.hostname 设置或取得 URL 中的主 ...

  6. Dolphin for Android(v11.5.1[Jetpack:内置])

    1. 下载的地址为“http://www.techspot.com/downloads/5927-dolphin-browser-for-android.html” ZC: 由于 Google Pla ...

  7. ORACLE 总结

    1. diagnostic file(alertlog, tracefile, redolog), 监控数据库动作时间点 [troubleshooting] alertlog : 确认checkpoi ...

  8. C++——将成员函数作为参数

    在C++中,成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的,首先 我们来回顾一下普通函数指针的传递方法: //------------------------------------- ...

  9. 【服务器环境搭建-Centos】tmpfs,【转载】

    转载来源:http://www.linuxidc.com/Linux/2013-12/93747.htm tmpfs介绍 tmpfs是一种虚拟内存文件系统,而不是块设备.是基于内存的文件系统,创建时不 ...

  10. The Zen Programmer (zhuan)

    http://blog.csdn.NET/marksinoberg/article/details/52460725 ***************************************** ...