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. 为运算表达式设计优先级 题目大意 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 + - * 思路: ...
随机推荐
- .net excel 导入 导出
哎,好好的代码今天说来个实验,结果用的是office15 气死人了,网上最高office14.dll 文章转自2012年 QQ群:13615607 MR.Young protected void Bt ...
- 编程挑战JavaScript进阶篇(慕课网题目)
编程挑战 现在利用之前我们学过的JavaScript知识,实现选项卡切换的效果. 效果图: 文字素材: 房产: 275万购昌平邻铁三居 总价20万买一居 200万内购五环三居 140万安家东三环 ...
- Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:jar:2.5.1
Mac上写了一段基于Maven的java代码. 上传Git后,在windows上pull下来,eclipse里面各种错误. ArtifactTransferException:Failure to t ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- docker之启动创建容器流程
libcontainer的工作流程 execdriver的run方法通过docker daemon提交一份command信息创建了一份可供libcontainer解读的容器配置container,继而 ...
- PHP11 日期和时间
学习要点 UNIX时间戳 将其他格式的日期转成UNIX时间戳格式 基于UNIX时间戳的日期计算 获取并格式化输出日期 修改PHP的默认时间 微秒的使用 Unix时间戳 相关概念 Unix tim ...
- HTML基础(五)表单
表单的工作原理 简单来说就是客户在浏览器输入信息之后,浏览器将用户在表单中的数据进行打包发送给服务器,服务器接收到之后进行处理,如下图 语法 <form> 表单元素</form> ...
- spring boot 在idea中实现热部署
1)在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- ios 封装sqllite3接口
2013-03-25 17:41 261人阅读 评论(0) 收藏 举报 // #import <Foundation/Foundation.h> #import "sqlite3 ...
- cf上分的失落->高兴->更失落
cf昨日做出一个题居然div2打了1800多名直接上分了...我原以为垂直落地但是....我现在1399差一分就能蓝名了啊啊啊啊,以后不一定会有这个水平的啊,给个蓝名体验卡不行吗,多加一分会死啊... ...