(medium)LeetCode 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"
.
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]
Example 2
Input: "2*3-4*5"
(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
Output: [-34, -14, -10, -10, 10]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解法思想:递归
代码如下:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret=new LinkedList<Integer>();
int len=input.length();
for(int i=0;i<len;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(Integer p1:part1Ret){
for(Integer p2:part2Ret){
int c=0;
switch(input.charAt(i)){
case '+':c=p1+p2;
break;
case '-':c=p1-p2;
break;
case '*': c=p1*p2;
}
ret.add(c);
}
}
}
}
if(ret.size()==0){
ret.add(Integer.valueOf(input));
}
return ret;
}
}
运行结果:
(medium)LeetCode 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 ...
- [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 (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- [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为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- 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]* ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
随机推荐
- python数据库操作pymysql
安装数据库: pip3 install pymysql 进行数据库的更新.插入.查询等操作: #!/usr/bin/python3.4 # -*- coding: utf-8 -*- #------- ...
- 邮件发送工具类 SendMail.java
package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...
- VBA実績表
VBA 插入一行保留样式 VBA 打开一个string指定的文件 VBA 按照文件类型名称打开一个文件 VBA excel中表示列的字母换成数字
- HTML5新事物
1 指定编码字符集,极力推荐 <meta charset="utf-8"> 2 指定lang,所有的标签上都有,推荐在<html>上指定. 3 css样式的 ...
- oc-类目、延展、协议
-----------------------------------------------Category-------------------------------------- 类目 是在原 ...
- Visual对象之DrawingContext.DrawRectangle在有的状态下似乎并不能提高性能
很多书上都提到使用Visual对象绘制图形可以提高绘图效率,但是经过本人亲测,结果却发现DrawingContext.DrawRectangle的效率远低于使用UIElement.Children.A ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- TMS320C54x系列DSP的CPU与外设——第8章 流水线
第8章 流水线 本章描述了TMS320C54x DSP流水线的操作,列出了对不同寄存器操作时的流水线延迟周期.(对应英语原文第7章) 8.1 流水线操作 TMS320C54x DSP有一个6段的指令流 ...
- 纯真IP根据IP地址获得地址
<?php /** * 纯真IP根据IP地址获得地址 */ class ipLocation { public $fp; public $firstip; //第一条ip索引的偏移地址 publ ...
- C++开发者都应该使用的10个C++11特性
转载自http://blog.jobbole.com/44015/ 在C++11新标准中,语言本身和标准库都增加了很多新内容,本文只涉及了一些皮毛.不过我相信这些新特性当中有一些,应该成为所有C++开 ...