题目:

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]

解法:分治法

思路:

分:遍历每个符号,分为符号前和后。如:"2-1-1-1",第1个‘-’分为 ‘2’ 和 ‘1-1-1’ ;第二个 '-' 分为 ‘2-1’ 和 ‘1-1’,第三个‘-’ 分为‘2-1-1’和‘1’

治:最后为一个数,则返回该数。

并:

  对于每一次的分,如【2-1*3】  -   【1-1*4】  ,a=‘2-1*3’ 和 b=‘1-1*4’, a,b都递归计算所有的可能结果存入一个数组中,遍历相加减乘。a有2种结果【3,-1】,b有两种结果【0,-3】,如果a和b之间是 ‘-’,则 a-b 就会返回4种结果。

代码如下:

    def diffWaysToCompute(input):
"""
:type input: str
:rtype: List[int]
"""
res=[]
result=0
for i,c in enumerate(input):
if c in "+-*":
for a in diffWaysToCompute(input[:i]):
for b in diffWaysToCompute(input[i+1:]):
if c=='+':
res.append(a+b)
elif c=='-':
res.append(a-b)
else:
res.append(a*b) return res or [int(input)]

算法8-----Different Ways to Add Parentheses(不同括号结果)的更多相关文章

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

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

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

  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. 241. Different Ways to Add Parentheses

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

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

  7. LC 241. Different Ways to Add Parentheses

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

  8. Different Ways to Add Parentheses

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

  9. 241. Different Ways to Add Parentheses——本质:DFS

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

随机推荐

  1. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...

  2. 百度之星2014资格赛 1003 - Xor Sum

    先上代码: Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  3. /tmp目录下执行脚本失败提示Permission denied

    Linux上执行Shell脚本运行失败提示Permission denied一个问题,挺好的问题,切中了知识盲点. 问题现象 Shell脚本在/tmp目录下,执行./test.sh运行失败,提示Per ...

  4. 洛谷 P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...

  5. git分支合并概念

    git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. git merge命令用于合并指定分支到当前分支. 创建与合并分支 阅读: 931277 在版本 ...

  6. poj2135(简单的最小费用流问题)

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. 125条常见的java面试、笔试题大汇总

    1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解所有问题,而仅仅是选择当中的一部分,临时不用部分细节.抽象包含两个方面,一是过程抽象. ...

  8. objective-c 中数据类型之四 字典(NSDictionary)

    // 1. 字典初始化.赋值方式1 NSMutableDictionary *m_dictionary = [[NSMutableDictionary alloc] initWithCapacity: ...

  9. Android开发策略:缓存

    1.使用缓存策略时,优先考虑使用sdcard(需先推断有无sd卡及其剩余空间是否足够,够的话就开辟一定空间如10M): 2.获取图片时.先从sdcard上找,有的话使用该图片并更新图片最后被使用的时间 ...

  10. Linux如何把以下文件夹修改为root权限?

    inux 修改文件目录所有者例:要将当前目录下名 title 的文件夹及其子文件的所有者改为geust组的su用户,方法如下:#chown -R su.geust title-R 递归式地改变指定目录 ...