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 ...
随机推荐
- 《Redis设计与实现》读书笔记
<Redis设计与实现>读书笔记 很喜欢这本书的创作过程,以开源的方式,托管到Git上进行创作: 作者通读了Redis源码,并分享了详细的带注释的源码,让学习Redis的朋友轻松不少: 阅 ...
- HTTP协议(转自:小坦克博客)
原文地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP ...
- python_way day11 自定义线程池
python_way day11 线程池 为什么需要线程池 线程多一些固然好,但是过多的线程反倒影响系统的负荷,所以我们就需要创建合适多的线程,哪我们把线程放到哪里?这时就放到线程池中. 线程池中存放 ...
- NPN&PNP
一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...
- codeforces 300E Empire Strikes Back 数论+二分查找
题意:给定N个数a1,a2,a3...aN,现在要求最小的n满足 n!/(a1!*a2!*...*aN!) 是一个正整数的最小的n. 分析:这题的想法很明确,就是分解a1!*a2!*...*aN!,把 ...
- mysql概要(十三)备份和恢复
1.采用mysql 自带备份命令: 数据库恢复:
- svn提交代码的原则
[1]先更新在提交 [2]多提交 [3]不要提交不能通过编译的代码 [4]每次提交必须书写明晰的标注 [5]提交时注意不要提交本地自动生成的文件 [6]不要提交自己不明白的代码 [7]慎用锁定功能
- ajax form表单回显
/* * 表单自动回显js * 依赖JQURY * 使用参考:$("#form1").form("load",{"id":"112 ...
- 转:从开源项目学习 C 语言基本的编码规则
从开源项目学习 C 语言基本的编码规则 每个项目都有自己的风格指南:一组有关怎样为那个项目编码约定.一些经理选择基本的编码规则,另一些经理则更偏好非常高级的规则,对许多项目而言则没有特定的编码规则,项 ...
- 转:UML类图几种关系的总结
转自:http://www.open-open.com/lib/view/open1328059700311.html 在UML类图中,常见的有以下几种关系: 泛化(Generalization), ...