241. Different Ways to Add Parentheses——本质:DFS
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的更多相关文章
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 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 ...
- 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]* ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 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 ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
随机推荐
- HDU 5833 Zhu and 772002
HDU 5833 Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- run a Freight robot (2)
3. Network Setup Connecting Freight to a Monitor The easiest way to configure the wireless networki ...
- 2016中国APP分类排行榜参选入围产品公示
2016中国APP分类排行榜参选入围产品公示 由中国科学院<互联网周刊>.中国社会科学院信息化研究中心.eNet硅谷动力共同主办的2016中国APP分类排行榜发布暨颁奖晚宴即将举行.此 ...
- Redis基础知识之————空间换时间的查询案例
空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于redis这个 ...
- Codeforces 713D Animals and Puzzle
题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...
- Monkey学习(2)简单命令合集
Monkey命令的简单帮助 执行所有命令的前提是,必须先链接模拟器或者实体机,否则会报如下错误信息: 打开命令行窗口,WIN+R,输入CMD 在命令行窗口执行:adb shell monkey –he ...
- Python IDE专用编辑器PyCharm下载及配置安装过程(Ubuntu环境)
这几天在折腾Python环境,显示把笔记本安装Ubuntu Linux环境系统,然后基本的Python环境都安装完毕之后需要安装传说中在其平台中最好的代码编辑和管理工具PyCharm,于是就根据网上的 ...
- sqlplus入门基础语句
关于Oracle 首先Oracle一个数据库由若干个表空间组成,每个表空间由若干个数据文件(或设备)组成,每个数据文件由若干个盘区组成,每个盘区由若干个block组成.这是Oracle的物理结构. 逻 ...
- python 在不同层级目录import 模块的方法
有一个文件夹/home/a, 里面有个模块叫b.py, 我怎么把他import到程序里? 1). import sys; sys.path.append("/home/a/") ...
- ZendStudio的配置导出
File(文件)->Export(导 出),再弹出Export窗口中点击"General(常规)",选择"Preferences(首选项)" 点击&quo ...