class Solution(object):
def diffWaysToCompute(self, input):
"""
:type input: str
:rtype: List[int]
"""
#一个函数calc做运算,
#一个字典memo记录已经有的结果,key为输入字符串,value为所有计算结果组合
#遍历input,以一个运算符为界,对左右两边进行计算,得到所有组合后返回,并进行排列组合计算
#递归边界为input为数字
if input.isdigit():
return [int(input)]
memo={}
res=[]
if input in memo:
return memo[input]
for i in range(len(input)):
op=input[i]
if op not in "+-*":
continue left=self.diffWaysToCompute(input[:i])
right=self.diffWaysToCompute(input[i+:])
for num1 in left:
for num2 in right:
res.append(self.calc(num1,num2,op))
memo[input]=res
return res def calc(self,num1,num2,op):
if op=="+":
return num1+num2
elif op=="-":
return num1-num2
else:
return num1*num2

leetcode241 为运算表达式设计优先级的更多相关文章

  1. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

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

  2. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  3. Java实现 LeetCode 241 为运算表达式设计优先级

    241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...

  4. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

    给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...

  5. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

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

  6. leetcode.分治.241为运算表达式设计优先级-Java

    1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...

  7. LeetCode:为运算表达式设置优先级【241】

    LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含  ...

  8. leetcode-241-为运算表达式设置优先级*

    题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...

  9. python学习之运算表达式优先级

    python中,有变量.值和运算符参与的语句叫做表达式. 比如: #字符串表达式 "hello" #运算表达式 + #赋值表达式 test = "hello" ...

随机推荐

  1. hadoop上hive的安装

    1.前言 说明:安装hive前提是要先安装hadoop集群,并且hive只需要再hadoop的namenode节点集群里安装即可(需要再所有namenode上安装),可以不在datanode节点的机器 ...

  2. 树莓派wifi环境下初始化及环境配置

    在此放一下我的系统 链接:https://pan.baidu.com/s/192cL6qSsMd-wqxHeDWfIug 提取码:0lrq 1.准备一张内存卡,最好是32G class10 16G的话 ...

  3. java_day01

    ch01: ===================================== java J2SE 桌面应用的开发 JAVA SE corejava J2EE 企业级开发 JAVA EE J2 ...

  4. 从FBV到CBV三(权限)

    丛FBC到CBV三(权限) span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0 ...

  5. oracle修改某个表的字段顺序

    有时候会发现某个表的列顺序不理想,想修改 -1查询表, select * from AIRWAY_TYPE t --2 查询用户和表名,找到obj#,select object_id from all ...

  6. ImportError: attempted relative import with no known parent package

    或者检查所导包是否存在__init__.py文件,没有则添加上即可使当前文件夹变为包.

  7. Floyed(floyd)算法详解

    是真懂还是假懂? Floyed算法:是最短路径算法可以说是最慢的一个. 原理:O(n^3)的for循环,对每一个中间节点k做松弛(寻找更短路径): 但它适合算多源最短路径,即任意两点间的距离. 但sp ...

  8. .NET界面控件DevExpress全新发布v19.1.5|改进Office 2019主题

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...

  9. js array map() 函数的简单使用

    语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值 index:可选.当前元素的索引值 ...

  10. 将Excel数据读入DataGridView

    OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Microsoft Excel ...