leetcode241 为运算表达式设计优先级

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 为运算表达式设计优先级的更多相关文章
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- Leetcode 241.为运算表达式设计优先级
为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...
- Java实现 LeetCode 241 为运算表达式设计优先级
241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- leetcode.分治.241为运算表达式设计优先级-Java
1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...
- LeetCode:为运算表达式设置优先级【241】
LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 ...
- leetcode-241-为运算表达式设置优先级*
题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...
- python学习之运算表达式优先级
python中,有变量.值和运算符参与的语句叫做表达式. 比如: #字符串表达式 "hello" #运算表达式 + #赋值表达式 test = "hello" ...
随机推荐
- 直通BAT必考题系列:JVM性能调优的6大步骤,及关键调优参数详解
JVM内存调优 对JVM内存的系统级的调优主要的目的是减少GC的频率和Full GC的次数. 1.Full GC 会对整个堆进行整理,包括Young.Tenured和Perm.Full GC因为需要对 ...
- namenode datanode理解
HDFS是以NameNode和DataNode管理者和工作者模式运行的. NameNode管理着整个HDFS文件系统的元数据.从架构设计上看,元数据大致分成两个层次:Name ...
- jq sku实现
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 线段树(SegmentTree)基础模板
线段树模板题来源:https://www.lintcode.com/problem/segment-tree-build/description 201. 线段树的构造 /** * Definitio ...
- k8spod控制器概述
自主式pod对象由调度器绑定至目标工作节点后即由相应节点上的kubelet负责监控其容器的存活性,容器主进程崩溃后,kubelet能够自动重启相应的容器.不过,kubelet对非主进程崩溃类的容器错误 ...
- Rest_Framework简介
Web应用模式 在开发Web应用中,有两种应用模式:前后端不分离和前后端分离 前后端不分离 前后端不分离通俗来讲就是不区分前端和后端,浏览器请求时服务器直接返回页面,其示意图如下 前后端分离 前后端分 ...
- javascript / angular 如何把object转成array
取出的api 格式是纯object格式 {"name":"james","city":"Taipei","co ...
- 对JS继承的研究--------------引用
问:类继承和原型继承不是同一回事儿吗,只是风格选择而已? 答:不是! 类继承和原型继承不论从本质上还是从语法上来说,都是两个截然不同的概念. 二者之间有着区分彼此的本质性特征.要完全看懂本文,你必须牢 ...
- socket认证客户端链接合法性
服务器端: #_*_coding:utf-8_*_ __author__ = 'Linhaifeng' from socket import * import hmac,os secret_key=b ...
- AngularJS 前端 MVC 的设计与搭建
代码 #未引入MVC框架之前的代码 <!doctype html> <html> <head> <meta charset="UTF-8" ...