LC 241. Different Ways to Add Parentheses
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的更多相关文章
- 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 ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 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]* ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 241. Different Ways to Add Parentheses——本质:DFS
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- 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 ...
随机推荐
- CAFFE(四):Ubuntu 下安装jupyter notebook
第一步.安装 pycaffe notebook 接口环境 在上一步成功安装 caffe 之后,就可以通过 caffe 去做训练数据集或者预测各种相关的事了,只不过需要在命令行下通过 caffe 命令进 ...
- WebApi接口测试工具
原文出处: 懒得安分 前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员也不懂C#语法,API里面的接口也不能直接给他们看,没办法,只有整个详细一点的文档呗. ...
- Centos使用光盘yum源
yum查看所有源 yum repolist all 方法一:本机使用光盘源安装软件的设置 mkdir /media/cdrom mount /dev/cdrom /media/cdrom vim / ...
- Python&Selenium借助HTMLTestRunner生成自动化测试报告
一.摘要 本篇博文介绍Python和Selenium进行自动化测试时,借助著名的HTMLTestRunner生成自动化测试报告 HTMLTestRunner.py百度很多,版本也很多,自行搜索下载放到 ...
- MySQL 5.6, 5.7, 8.0版本的新特性汇总大全
转载:http://blog.itpub.net/15498/viewspace-2650661/ MySQL 5.6 1).支持GTID复制 2).支持无损复制 3).支持延迟复制 4).支持基于库 ...
- zabbix内存溢出解决方法
1406:20180802:183248.783 __mem_malloc: skipped 0 asked 48 skip_min 4294967295 skip_max 0 1406:201808 ...
- 解决从其他地方拷贝过来的Android项目在本机不能运行(报错)的问题
这个问题一般是由gradle版本不同引起的.要解决可以这样: 一.在确保本机Android studio 正常使用的情况下,在本机新建一个Android项目 二.在文件夹中打开新建的Android项目 ...
- 装饰者模式(Decorator)---结构型
1 基础知识 定义:在不改变原有对象的基础上,将功能附加到对象上即动态地给一个对象添加一些额外的职责.特征:提供了比继承更有弹性的替代方案. 本质:动态组合. 使用场景:扩展一个类的功能或给一个类添加 ...
- 根据xml文件生成javaBean
原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...
- PHP mysqli_fetch_fields() 函数
mysqli_fetch_fields() 函数返回结果集中代表字段(列)的对象的数组. 返回结果集中代表字段(列)的对象的数组,然后输出每个字段名称.表格和最大长度: <?php // 假定数 ...