题目:

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]

解法:分治法

思路:

分:遍历每个符号,分为符号前和后。如:"2-1-1-1",第1个‘-’分为 ‘2’ 和 ‘1-1-1’ ;第二个 '-' 分为 ‘2-1’ 和 ‘1-1’,第三个‘-’ 分为‘2-1-1’和‘1’

治:最后为一个数,则返回该数。

并:

  对于每一次的分,如【2-1*3】  -   【1-1*4】  ,a=‘2-1*3’ 和 b=‘1-1*4’, a,b都递归计算所有的可能结果存入一个数组中,遍历相加减乘。a有2种结果【3,-1】,b有两种结果【0,-3】,如果a和b之间是 ‘-’,则 a-b 就会返回4种结果。

代码如下:

    def diffWaysToCompute(input):
"""
:type input: str
:rtype: List[int]
"""
res=[]
result=0
for i,c in enumerate(input):
if c in "+-*":
for a in diffWaysToCompute(input[:i]):
for b in diffWaysToCompute(input[i+1:]):
if c=='+':
res.append(a+b)
elif c=='-':
res.append(a-b)
else:
res.append(a*b) return res or [int(input)]

算法8-----Different Ways to Add Parentheses(不同括号结果)的更多相关文章

  1. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

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

  2. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

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

  3. 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 ...

  4. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  5. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

  6. 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]* ...

  7. LC 241. Different Ways to Add Parentheses

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

  8. Different Ways to Add Parentheses

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

  9. 241. Different Ways to Add Parentheses——本质:DFS

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

随机推荐

  1. 线上MySQL慢查询现象案例--Impossible WHERE noticed after reading const tables

    前言:2012年的笔记整理而得,发布个人博客,做备忘录使用. 背景:线上慢查询日志监控,得到如下的语句:       发现:select doc_text from t_wiki_doc_text w ...

  2. asp.net-EF-表间关系

    博客推荐 http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864150.html 先学习下这几个方法 Has方法: HasOptional:前者包含 ...

  3. [bzoj1090][SCOI2003]字符串折叠_区间dp

    字符串折叠 bzoj-1090 SCOI-2003 题目大意:我说不明白...链接 注释:自己看 想法:动态规划 状态:dp[i][j]表示从第i个字符到第j个字符折叠后的最短长度. 转移:dp[l] ...

  4. Oracle-表更名、转存数据

    --更名 ALTER TABLE T_LOGSRV_SERVICE RENAME TO T_LOGSRV_SERVICE_20170418_BAK; --创建同样的表 ;

  5. 一种加快在苹果app store中上架的方法

    预计近期苹果app应用上架的比較多,审核比較慢,如今一个app从提交到上架短则7.8天.长则2.3个星期.我在实际上线应用时,总结了一个简单有用的小技巧,能够加快上架时间,近期使用这样的方法后.我们基 ...

  6. oc23--变量修饰符

    // // Person.h #import <Foundation/Foundation.h> /* @public:所有类访问 @private:本类访问 @protected:本类子 ...

  7. 通用扩展函数之TypeParse

    代码实现: ".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); );//转换为int失败返回1 ); " ...

  8. C++ 指针 引用 变量引用

    变量引用: 引用的作用就是给变量起个别名,假如有一个变量a,想给它起个别名b,         可以这么写:int a;//定义a是整型变量.int &b=a;//声明b是a的引用. 上面就是 ...

  9. 转载【梦想天空(山边小溪)】Web 开发人员和设计师必读文章推荐【系列二十九】

    博客地址:http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-29.html

  10. 对JVM还有什么不懂的?一文章带你深入浅出JVM!

    本文跟大家聊聊JVM的内部结构,从组件中的多线程处理,JVM系统线程,局部变量数组等方面进行解析 JVM JVM = 类加载器(classloader) + 执行引擎(execution engine ...