题目链接:https://leetcode.com/submissions/detail/86532557/

算法类型:分治法

题目分析:计算表达式的所有结果可能性

代码实现:

 class Solution(object):
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
def dfs(s, cache) :
ops = {'+':lambda x,y:x+y, '-':lambda x,y:x-y, '*':lambda x,y:x*y}
if not cache.has_key(s) :
ret = []
for k, v in enumerate(s) :
if v in '+-*' :
for left in dfs(s[:k], cache) :
for right in dfs(s[k+1:], cache) :
ret.append(ops[v](left,right))
if not ret :
ret.append(int(s))
cache[s] = ret
return cache[s] return dfs(input, {})

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

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

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

  5. 241. Different Ways to Add Parentheses

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

  6. LC 241. Different Ways to Add Parentheses

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

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

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

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

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

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

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

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

随机推荐

  1. Mac卸载MySQL

    打开终端输入以下指令即可 sudo rm -rf /Library/StartupItems/MySQLCOM sudo rm -rf /Library/PreferencePanes/My* rm ...

  2. 使用python的Flask实现一个RESTful API服务器端[翻译]

    最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...

  3. 理解HTML语义化

    1.什么是HTML语义化? <基本上都是围绕着几个主要的标签,像标题(H1~H6).列表(li).强调(strong em)等等> 根据内容的结构化(内容语义化),选择合适的标签(代码语义 ...

  4. node基础12:动态网页

    1.显示动态网页 又到了激动人心的时刻,马上就可以使用node创建动态网站了,其原理为: 在HTML模板中使用占位符 根据请求路径,确定需要返回的页面 根据请求参数来确定静态模板中占位符的值 使用正则 ...

  5. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  6. xwalk_core_library-15.44.384 .13.aar 百度云分享

    xwalk_core_library-15.44.384.13.aar 这玩意下载很慢 特意放到百度,需要的可以通过百度下载 http://pan.baidu.com/s/1nt0Cmbn 下完后放到 ...

  7. maven配置和下载

    下载链接:http://maven.apache.org/docs/ 以maven3.0.4为例,eclipse以kepler为例 环境变量的配置 1.系统变量-新建-变量名:MAVEN_HOME-变 ...

  8. 【URAL 1519】Formula 1

    http://acm.timus.ru/problem.aspx?space=1&num=1519 调了好久啊.参考(抄)的iwtwiioi的题解. 如果想要题解,题解在<基于连通性状态 ...

  9. SQLMap Tamper Scripts Update 04/July/2016

    SQLMap Tamper Scripts Update apostrophemask.py Replaces apostrophe character with its UTF-8 full wid ...

  10. css 通用reset,common

    /*------------------------------------*\ $RESET \*------------------------------------*/ body, div, ...