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. C#中Application.DoEvents()的作用

    Visual Studio里的摘要:处理当前在消息队列中的所有 Windows 消息. 交出CPU控制权,让系统可以处理队列中的所有Windows消息,比如在大运算量循环内,加Application. ...

  2. php 怎么设置报错级别 和 控制报错[转]

    在Windows环境下:有时在其他环境下运行正常的程序在自己的环境上会报错误    程序会 报出  Undefined index:   这样的错误例如有如下的代码:                  ...

  3. list map vector set 常用函数列表

    #include <stdio.h> #include <iostream>//cin,cout #include <sstream>//ss transfer. ...

  4. otl插入数据不成功

    原因是:void rlogon(...); 没有设置auto_commit为1,otl不会自动提交. 注意:static int otl_initialize (const int threaded_ ...

  5. Python中的__new__()方法的使用

    __new__() 函数只能用于从object继承的新式类. 先看下object类中对__new__()方法的定义: class object:   @staticmethod # known cas ...

  6. uva 10692 Huge Mods 超大数取模

    vjudge上题目链接:Huge Mods 附上截图: 题意不难理解,因为指数的范围太大,所以我就想是不是需要用求幂大法: AB % C = AB % phi(C) + phi(C) % C ( B ...

  7. 【摘抄】meta系列用法总结【持续更新中】

    meta标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). ★页面描述信息NAME变量  name是描述网页的,对应于Content(网页内容),以便于搜索引擎机器人 ...

  8. eclipse里maven install时,报错提示jdk为无效的目标版本:1.7

    http://blog.csdn.net/wabiaozia/article/details/51733372 ************************************ 报错提示: [ ...

  9. UnicodeEncodeError

    UnicodeEncodeError at /admin/shop/product/add/ 'ascii' codec can't encode characters in position 0-1 ...

  10. Lua a and b or c

    lua中nil和false为条件不成立,其余都为条件成立. a and b : a条件不成立,则返回a,否则,返回b a or c   : a条件成立,则返回a,否则,返回b 常用x = x or v ...