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"
Output: [0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2

Example 2:

Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation:
(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

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

class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret = new ArrayList<>();
for(int i=0; i<input.length(); i++){
if(input.charAt(i) == '+' ||
input.charAt(i) == '-' ||
input.charAt(i) == '*') {
String part1 = input.substring(0,i);
String part2 = input.substring(i+1);
List<Integer> part1ret = diffWaysToCompute(part1);
List<Integer> part2ret = diffWaysToCompute(part2);
for(int x : part1ret){
for(int y : part2ret){
switch (input.charAt(i)) {
case '+' : ret.add(x + y);
break;
case '-' : ret.add(x - y);
break;
case '*' : ret.add(x * y);
break;
}
}
}
}
}
if(ret.size() == 0) ret.add(Integer.valueOf(input));
return ret;
} }

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. 【3】Git命令

    个人推荐的Git知识学习网站:https://git-scm.com . git常用操作图 init -> add -> commit -> remote -> push 初始 ...

  2. Mysql(四):数据操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  3. web开发:javascript案例

    一.浮动与定位复习 二.小米菜单案例 三.轮播图 四.滚动轮播 一.浮动与定位复习 - 浮动与相对定位 ```js// 1.两者均参与布局// 2.主浮动布局, 相对布局辅助完成布局微调// 3.相对 ...

  4. (转)为什么收到三个重复的ACK意味着发生拥塞?

    三次重复的ACK,可能是丢包引起的,丢包可能是网络拥塞造成的,也可能是信号失真造成的. 三次重复的ACK,也有可能是乱序引起的,而乱序和网络拥塞没有直接关系. 如果就写这两行,感觉什么都没写,接下来的 ...

  5. CSS3 -- 边框圆角

    文章后有彩蛋哦 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. (二)线程Thread中的方法详解

    1.start() start()方法的作用讲得直白点就是通知"线程规划器",此线程可以运行了,正在等待CPU调用线程对象得run()方法,产生一个异步执行的效果.通过start( ...

  7. MessageBox和ShellExecute初体验

    #include <stdio.h> //包含头文件,标准输入输出库 #include <windows.h> //包含windows头文件,ShellExecute正来自于此 ...

  8. @Configuration的使用 和作用(转)

    从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplic ...

  9. MySQL Workbench 导入导出乱码解决方法

    1.点击导出 2.默认选择cvs 3.打开后发现乱码 4.用记事本的方式打开会发现编码正常 5.文件->另存为,会发现编码为UTF-8,正是MySQL表的编码方式.我们选择编码方式为ANSI,保 ...

  10. App自动化-python基础

    定义类:类变量.成员变量.局部变量:构造函数.类方法:实例化对象: # -*- coding: utf-8 -*- ''' Created on 2019-6-25 @author: adminstr ...