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. springboot开启事务管理

    spring中开启事务管理需要在xml配置文件中配置,springboot中采取java config的配置方式. 核心是@EnableTransactionManager注解,该注解即为开启事务管理 ...

  2. PHP 几种 序列化/反序列化的方法

    序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...

  3. What is the difference between application server and web server?

    http://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web- ...

  4. [已解决]window下Can't connect to MySQL server on 'localhost' (10061)与无法启动MYSQL服务”1067 进程意外终止”

    查了一圈,发现都解决不了我的问题,查了 window 系统日志,提示缺少了某些文件,文件怎么丢的我也不知道,以下是解决办法. -- 我的 mysql 版本为 5.6.x 压缩包版本,我的 mysql ...

  5. Android Notification使用方法

    1.http://www.cnblogs.com/plokmju/p/android_Notification.html 2.http://blog.csdn.net/vipzjyno1/articl ...

  6. Web应用程序使用Hibernate

    在本文中,我们将学习使用hibernate创建一个Web应用程序. 对于创建Web应用程序,我们使用JSP表示逻辑层,使用Bean类表示数据,以及使用DAO类操作数据库.在hibernate中创建简单 ...

  7. JavaScript中trim 方法实现

    Java中的 String 类有个trim() 能够删除字符串前后的空格字符.jQuery中也有trim()方法能够删除字符变量前后的字符串. 可是JavaScript中却没有对应的trim() 方法 ...

  8. day20常用模块

    一.正则内容的补充 import re # ret = re.findall(r'www\.baidu\.com|www\.oldboy\.com','www.baidu.com') # # ret ...

  9. php通过post将表单数据保存到数据库实例

    html的form表单 <form id="contact-form" method="POST" action="../php/msg.php ...

  10. [Spring MVC]学习笔记--@RequestMapping支持的返回类型

    下面针对官方文档列出的支持类型进行举例. (本篇例子存于github上, https://github.com/lemonbar/spring-mvc-requestmapping) 可以直接下载, ...