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. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  2. 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...

  3. PHP修改memory_limit的三种办法

     PHP修改memory_limit的三种办法 2010-06-11 10:57:11 分类: 可能是分词程序的问题.只要搜索的字段达到十个汉字以上,就会出现诸如以下的错误 Fatal error: ...

  4. Java 十进制和十六制之间的转化(负数的处理)

    原文: http://www.cnblogs.com/literoad/archive/2013/01/25/2875908.html 在一些情况下,我们需要将数字在十进制和十六制下互相转化. 在Ja ...

  5. jquery中Uncaught TypeError: $(...).ajaxUpload is not a function(…)错误解决方法

    错误原因:该函数不是jquery的核心函数,所以需要外部引入ajaxfileupload.js文件,可能是没有引入,或者引入的js文件互相冲突 解决方法:每次进入一个函数之前打印该函数所有的js文件, ...

  6. java中static变量的声明和初始化

     目录(?)[+] 问题1静态变量如何初始化 问题2JDK如何处理static块 问题3如何看待静态变量的声明 对初始问题的解答 在网上看到了下面的一段代码: public class Test  ...

  7. UICollectionView的简单认识和简单实用

    摘要 UICollectionView是比UITableView更加复杂的UI控件,通过它可以实现许多复杂的流布局.但对我们来说,系统提供的接口十分简单易用,并且有十分强的制定性. iOS流布局UIC ...

  8. Vsphere笔记06 Vcenter 部署流程 1

    Vcenter 部署流程 1   一.环境需求   1.需要两台装着WIN2K8 R2 64X的服务器   2.启用一台要添加活动目录角色,并且配置DC,DC的参数如下: 域名:justech-dc. ...

  9. redis的下载

    网址一:https://github.com/dmajkic/redis/downloads 网址二:http://windows.php.net/downloads/pecl/releases/re ...

  10. Xcode7.3打包ipa文件 报错和解决

    An error occurred during export. The file “xxx.ipa” couldn’t be opened because there is no such file ...