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
采用分治算法,分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同,求出子问题的解,就可得到原问题的解。那么针对本题,以操作符为分界,将字符串分解为较小的两个子字符串,然后依次对两个子字符串进行同样的划分,直到字符串中只含有数字。再根据操作符对两端的数字进行相应的运算。
由于原问题和子问题中操作符不止有一个,那么就需要在原问题和子问题中循环找到这个操作符,进行同样的划分:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new ArrayList<Integer>();
for (int i=0; i<input.length(); i++) {
char ch = input.charAt(i);
if (ch == '+' || ch == '-' || ch == '*') {
List<Integer> left = diffWaysToCompute(input.substring(0,i));
List<Integer> right = diffWaysToCompute(input.substring(i+1,input.length()));
for (int l : left) {
for (int r : right) {
switch(ch) {
case '+' :
res.add(l+r);
break;
case '-' :
res.add(l-r);
break;
case '*' :
res.add(l*r);
break;
}
}
}
}
}
if (res.size() == 0) res.add(Integer.valueOf(input));
return res;
}
}
显然上述解法对子问题存在重复计算,效率不高,这里采用备忘录的自顶向下法,将子问题的计算结果保存下来,下次遇到同样的子问题就直接从备忘录中取出,而免去繁琐的计算,具体的做法是新建一个 hashmap,将子字符串放入 hashmap 中,对应的计算结果放入 value 中:
public class Solution {
private HashMap<String, List<Integer>> hm = new HashMap<String, List<Integer>>();
public List<Integer> diffWaysToCompute(String input) {
if(hm.containsKey(input)) return hm.get(input);
List<Integer> res = new ArrayList<Integer>();
for (int i=0; i<input.length(); i++) {
char ch = input.charAt(i);
if (ch=='+' || ch=='-' || ch=='*')
for (Integer l : diffWaysToCompute(input.substring(0,i)))
for (Integer r : diffWaysToCompute(input.substring(i+1,input.length())))
if(ch=='+') res.add(l+r);
else if (ch == '-') res.add(l-r);
else res.add(l*r);
}
if (res.size() == 0) res.add(Integer.valueOf(input));
hm.put(input, res);
return res;
}
}
Leetcode 241.为运算表达式设计优先级的更多相关文章
- Java实现 LeetCode 241 为运算表达式设计优先级
241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...
- leetcode.分治.241为运算表达式设计优先级-Java
1. 具体题目 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: & ...
- LeetCode:为运算表达式设置优先级【241】
LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 ...
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
- leetcode241 为运算表达式设计优先级
class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...
- leetcode-241-为运算表达式设置优先级*
题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...
- LeetCode.241
241. 为运算表达式设计优先级 题目大意 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 + - * 思路: ...
随机推荐
- Contextual Action bar(1) CAB in Android
Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...
- C++类成员函数与成员变量的内存布局
一.成员函数 成员函数可以被看作是类作用域的全局函数,不在对象分配的空间里,只有虚函数才会在类对象里有一个指针,存放虚函数的地址等相关信息.
- 让搜狗输入法更符合编程/vim使用的配置
1. “菜单”—“设置属性”—“常用”—“初始状态”里的“中/英文”选项,选中“英文” 2. 设置属性里的“高级”里的“高级模式”,点“英文输入法设置”,“启动时启用英文输入法”选中 3. 按键-中英 ...
- LN : leetcode 730 Count Different Palindromic Subsequences
lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...
- 基于socketserver实现的并发(tcp和udp)
threading 线程 基于tcp协议:请求建立连接,然后开启进程 基于udp协议:直接开启新进程 基于tcp协议 import socketserver # 导入socketserver模块 # ...
- datetime 模块详解
1.import datetime 常用方法: ttimedelta() 括号里默认为days,进行别的单位运算可以加上如hours = 1这样.除了进行减法运算,还可以进行加法运算. >> ...
- <meta>详解
一.元数据和<meta> 元数据是描述以提供关于其他数据的数据,在<meta>中,html document是被描述的数据,meta标签中包括的数据是描述html docume ...
- (转)新手学习System Verilog & UVM指南
从刚接触System Verilog以及后来的VMM,OVM,UVM已经有很多年了,随着电子工业的逐步发展,国内对验证人才的需求也会急剧增加,这从各大招聘网站贴出的职位上也可以看出来,不少朋友可能想尽 ...
- 磁盘格式化mke2fs
-b 设置每个块的大小,当前支持1024,2048,40963种字节 -i 给一个inode多少容量 -c 检查磁盘错误,仅执行一次-c时候,会进行快速读取测试:-c -c会测试读写,会很慢 -L 后 ...
- 浅谈Link-Cut Tree(LCT)
0XFF 前言&概念 Link-Cut Tree 是一种用来维护动态森林连通性的数据结构,适用于动态树问题.它采用类似树链剖分的轻重边路径剖分,把树边分为实边和虚边,并用 Splay 来维护每 ...