LeetCode:为运算表达式设置优先级【241】

题目描述

给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +- 以及 * 。

示例 1:

输入: "2-1-1"
输出: [0, 2]
解释:
((2-1)-1) = 0
(2-(1-1)) = 2

示例 2:

输入: "2*3-4*5"
输出: [-34, -14, -10, -10, 10]
解释:
(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

题目分析

  这道题的核心概念是找一个位置去分割表达式,但是这里的位置只能是操作符(+、-、*),把原来的表达式分割成为两个子表达式,先分别求解两个子表达式的值,接着子表达式递归求解出来的值的集合根据操作符做笛卡尔积。

  例如:

    

  例如:

  

  这道题真的应该反思一下,题目中体现的使用括号来实现优先级,但是其实呢,他其实就是考虑了所有的可能性,就跟全排列一样,要求的就是所有结果值。我们使用分治思想,围绕运算符一层一层向下做分割,分割到最后其实就是一个个具体的值,然后在向上左笛卡尔积,并把所有的取值加入到结果中来。

Java题解

 class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new ArrayList<>();
for(int i = 0;i<input.length();i++)
{
char c = input.charAt(i);
if(c=='+'||c=='-'||c=='*')
{
String partLeft = input.substring(0,i);
String partRight = input.substring(i+1);
List<Integer> resLeft = diffWaysToCompute(partLeft);
List<Integer> resRight = diffWaysToCompute(partRight);
for(Integer intLeft:resLeft)
for(Integer intRight:resRight)
{
if(c=='+')
res.add(intLeft+intRight);
if(c=='-')
res.add(intLeft-intRight);
if(c=='*')
res.add(intLeft*intRight);
}
}
}
if(res.size()==0)
res.add(Integer.valueOf(input));
return res;
}

LeetCode:为运算表达式设置优先级【241】的更多相关文章

  1. leetcode-241-为运算表达式设置优先级*

    题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...

  2. Java实现 LeetCode 241 为运算表达式设计优先级

    241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...

  3. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  4. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  5. leetcode.分治.241为运算表达式设计优先级-Java

    1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...

  6. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

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

  7. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

    给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...

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

    class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...

  9. python学习之运算表达式优先级

    python中,有变量.值和运算符参与的语句叫做表达式. 比如: #字符串表达式 "hello" #运算表达式 + #赋值表达式 test = "hello" ...

随机推荐

  1. 文件操作接口的系统调用分析---SYSCALL_DEFINEx

    linux/arch/arm/kernel/call.S ... CALL(sys_read) CALL(sys_write) CALL(sys_open) CALL(sys_close) ...   ...

  2. STL next_permutation(a,a+n) 生成一个序列的全排列。满足可重集。

    /** 题目: 链接: 题意: 思路: */ #include <iostream> #include <cstdio> #include <vector> #in ...

  3. select option 不可以选

    <select> <option>Volvo</option> <option>Saab</option> <option disab ...

  4. GoogleMap-------manifest文件配置

    前言:在使用GoopleMap之前需要配置manifest文件 1.这个可有可无,com.xhm.meishi是项目的包名 <!-- 声明调用这个应用需要的权限 --> <permi ...

  5. cookie小细节

    设置cookie时,不像设置session,可以马上生效,它的生效时间是下一次请求页面.

  6. urllib -- ProxyHandler处理器(代理设置)

    import urllib.requestimport randomimport ssl proxy_list = [ {"https" : "196.61.27.58: ...

  7. UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案

    本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertControlle ...

  8. iOS学习笔记(六)——ViewController

    ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...

  9. 3280 easyfinding

    3280 easyfinding  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给一个M ...

  10. KVC示例

    KVC –key value Coding,可以让我们通过键值编码的形式进行属性值的赋值 参考苹果官网的图.. 1.KVC 定义一个Person类 .h文件 1: #import <Founda ...