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:

  1. Input: "2-1-1"
  2. Output: [0, 2]
  3. Explanation:
  4. ((2-1)-1) = 0
  5. (2-(1-1)) = 2

Example 2:

  1. Input: "2*3-4*5"
  2. Output: [-34, -14, -10, -10, 10]
  3. Explanation:
  4. (2*(3-(4*5))) = -34
  5. ((2*3)-(4*5)) = -14
  6. ((2*(3-4))*5) = -10
  7. (2*((3-4)*5)) = -10
  8. (((2*3)-4)*5) = 10

Runtime: 4 ms, faster than 77.90% of Java online submissions for Different Ways to Add Parentheses.

  1. class Solution {
  2. public List<Integer> diffWaysToCompute(String input) {
  3. List<Integer> ret = new ArrayList<>();
  4. for(int i=0; i<input.length(); i++){
  5. if(input.charAt(i) == '+' ||
  6. input.charAt(i) == '-' ||
  7. input.charAt(i) == '*') {
  8. String part1 = input.substring(0,i);
  9. String part2 = input.substring(i+1);
  10. List<Integer> part1ret = diffWaysToCompute(part1);
  11. List<Integer> part2ret = diffWaysToCompute(part2);
  12. for(int x : part1ret){
  13. for(int y : part2ret){
  14. switch (input.charAt(i)) {
  15. case '+' : ret.add(x + y);
  16. break;
  17. case '-' : ret.add(x - y);
  18. break;
  19. case '*' : ret.add(x * y);
  20. break;
  21. }
  22. }
  23. }
  24. }
  25. }
  26. if(ret.size() == 0) ret.add(Integer.valueOf(input));
  27. return ret;
  28. }
  29.  
  30. }

LC 241. Different Ways to Add Parentheses的更多相关文章

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

  2. 241. Different Ways to Add Parentheses

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

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

  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. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

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

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

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

  7. (medium)LeetCode 241.Different Ways to Add Parentheses

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

  8. [LeetCode#241]Different Ways to Add Parentheses

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

  9. leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)

    https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...

随机推荐

  1. CAFFE(四):Ubuntu 下安装jupyter notebook

    第一步.安装 pycaffe notebook 接口环境 在上一步成功安装 caffe 之后,就可以通过 caffe 去做训练数据集或者预测各种相关的事了,只不过需要在命令行下通过 caffe 命令进 ...

  2. WebApi接口测试工具

    原文出处: 懒得安分 前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员也不懂C#语法,API里面的接口也不能直接给他们看,没办法,只有整个详细一点的文档呗. ...

  3. Centos使用光盘yum源

    yum查看所有源 yum repolist all 方法一:本机使用光盘源安装软件的设置 mkdir /media/cdrom mount /dev/cdrom  /media/cdrom vim / ...

  4. Python&Selenium借助HTMLTestRunner生成自动化测试报告

    一.摘要 本篇博文介绍Python和Selenium进行自动化测试时,借助著名的HTMLTestRunner生成自动化测试报告 HTMLTestRunner.py百度很多,版本也很多,自行搜索下载放到 ...

  5. MySQL 5.6, 5.7, 8.0版本的新特性汇总大全

    转载:http://blog.itpub.net/15498/viewspace-2650661/ MySQL 5.6 1).支持GTID复制 2).支持无损复制 3).支持延迟复制 4).支持基于库 ...

  6. zabbix内存溢出解决方法

    1406:20180802:183248.783 __mem_malloc: skipped 0 asked 48 skip_min 4294967295 skip_max 0 1406:201808 ...

  7. 解决从其他地方拷贝过来的Android项目在本机不能运行(报错)的问题

    这个问题一般是由gradle版本不同引起的.要解决可以这样: 一.在确保本机Android studio 正常使用的情况下,在本机新建一个Android项目 二.在文件夹中打开新建的Android项目 ...

  8. 装饰者模式(Decorator)---结构型

    1 基础知识 定义:在不改变原有对象的基础上,将功能附加到对象上即动态地给一个对象添加一些额外的职责.特征:提供了比继承更有弹性的替代方案. 本质:动态组合. 使用场景:扩展一个类的功能或给一个类添加 ...

  9. 根据xml文件生成javaBean

    原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...

  10. PHP mysqli_fetch_fields() 函数

    mysqli_fetch_fields() 函数返回结果集中代表字段(列)的对象的数组. 返回结果集中代表字段(列)的对象的数组,然后输出每个字段名称.表格和最大长度: <?php // 假定数 ...