【LEETCODE】241-Different Ways to Add Parentheses
来源:互联网 发布:拜尔电动牙刷知乎 编辑:程序博客网 时间:2024/05/21 19:50
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]
参考:
http://bookshadow.com/weblog/2015/07/27/leetcode-different-ways-add-parentheses/
class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rtype: List[int] """ return [a+b if c=='+' else a-b if c=='-' else a*b for i,c in enumerate(input) if c in '+-*' for a in self.diffWaysToCompute(input[:i]) for b in self.diffWaysToCompute(input[i+1:]) ] or [int(input)]
0 0
- leetcode 241 Different Ways to Add Parentheses
- Leetcode 241- Different Ways to Add Parentheses
- Leetcode #241 Different Ways to Add Parentheses
- [leetcode 241]Different Ways to Add Parentheses
- LeetCode(241)Different Ways to Add Parentheses
- leetcode 241: Different Ways to Add Parentheses
- [LeetCode 241] Different Ways to Add Parentheses
- leetcode 241:Different Ways to Add Parentheses
- 【LEETCODE】241-Different Ways to Add Parentheses
- LeetCode 241 Different Ways to Add Parentheses
- LeetCode 241 - Different Ways to Add Parentheses
- LeetCode(241)Different Ways to Add Parentheses
- Leetcode #241 Different Ways to Add Parentheses
- LeetCode #241: Different Ways to Add Parentheses
- LeetCode #241 Different Ways to Add Parentheses
- LeetCode - Different Ways to Add Parentheses
- [leetCode] Different Ways to Add Parentheses
- Leetcode|Different Ways to Add Parentheses
- 基于Source和正则表达式的Scala网页内容抓取
- Ubuntu 下用NDK编译移植 FFmpeg 2.0 (配置最新版x264) 到Android平台
- Mybatis 动态sql
- iOS开发 打印CGRect时用NSStringFromCGRect
- 【Codeforces Round 273 (Div 2)C】【贪心 脑洞】 Table Decorations 三种气球装饰桌子 同桌三个气球不完全同色的最多桌子装饰数
- 【LEETCODE】241-Different Ways to Add Parentheses
- 转载一篇android裁剪图片的解决方案,个人呢觉得很好的
- 基于暗通道去雾算法的实现与优化(二)opencv在pc上的实现
- Codeforces Round #335 (Div. 2) 606C Sorting Railway Cars(hash)
- duilib获取CSliderUI滑动事件 - 用自定义控件实现
- IOS-42-谓词的使用(例:检查是否为手机号)
- 151211Exception : Could not load NIB in bundle
- C++学习笔记(初级篇)——作用域与“extern”关键字
- 简单易作的晶振测试