1. 具体题目

给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。

示例 1:  输入: "2-1-1"  输出: [0, 2]  解释:  ((2-1)-1) = 0;(2-(1-1)) = 2

2. 思路分析

分治 + 递归

3. 代码

 public List<Integer> diffWaysToCompute(String input) {
List<Integer> ans = new ArrayList<>();
for(int i = 0; i < input.length(); i++){
char c = input.charAt(i);
if(c == '+' || c == '-' || c == '*'){
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i + 1));
for(int l : left){
for(int r : right){
if(c == '+') {
ans.add(l + r);
}else if (c == '-') {
ans.add(l - r);
}else{
ans.add(l * r);
}
}
}
}
}
if(ans.size() == 0){
ans.add(Integer.valueOf(input));
}
return ans;
}

leetcode.分治.241为运算表达式设计优先级-Java的更多相关文章

  1. Java实现 LeetCode 241 为运算表达式设计优先级

    241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...

  2. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  3. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  4. [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  5. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

    给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...

  6. leetcode241 为运算表达式设计优先级

    class Solution(object): def diffWaysToCompute(self, input): """ :type input: str :rty ...

  7. LeetCode:为运算表达式设置优先级【241】

    LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含  ...

  8. leetcode-241-为运算表达式设置优先级*

    题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...

  9. leetcode算法学习----逆波兰表达式求值(后缀表达式)

    下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目:  有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...

随机推荐

  1. nodejs,事件轮询总结

    宏任务 script,setTimeoout,setInterval,setlmmediate(node 独有),I/o,render渲染 微任务 process.nextTick(),promise ...

  2. Solr的学习使用之(一)部署

    Solr的主要功能是全文检索,该功能分为两个过程:创建索引和对索引进行搜索 一.心得体会 第一次写技术博客,这次写的基本上都是从网络上整理的来的,外加自己的一些实践,以后争取全部原创哈,都说写技术博客 ...

  3. python常用函数 F

    filter(callable, list/tuple) 接收一个函数和一个序列,完成元素过滤. 例子: fnmatch(str,str) 使用底层操作系统的大小写敏感规则来匹配模式. 例子: fnm ...

  4. 莫比乌斯反演/线性筛/积性函数/杜教筛/min25筛 学习笔记

    最近重新系统地学了下这几个知识点,以前没发现他们的联系,这次总结一下. 莫比乌斯反演入门:https://blog.csdn.net/litble/article/details/72804050 线 ...

  5. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  6. Git命令<转载 https://www.cnblogs.com/cspku/articles/Git_cmds.html>

    查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...

  7. Mac brew 安装Postgres 开机自启动

    以下所有命令在mac 终端执行 1.安装postgres brew install postgres 2.brew 安装的程序都可以在/usr/local/Cellar/下找到,去/usr/local ...

  8. C# 微信公众号开发本地IIS调试

    由于最近公司需要自己开发微信,这个任务也就交给我了,由于第一次接触开发微信,所以也踩了不少坑,不过园子里文章也很多,也借鉴了不少文章,弯路也是少走了不少,现在将我自己踩的坑或者一些经验留下来,希望能帮 ...

  9. Python3.5-20190529-os模块

    os.getcwd() 获取当前路径os.listdir("路径") 返回该路径下面所有的文件os.path.abspath(path):返回path的绝对路径.os.path.s ...

  10. 什么是网站TDK?

    什么是网站TDK?可能很多新手站长与SEOer不知道.其实TDK就是网站的标题(title).描述(description)和关键词(keyword),TDK是网站很重要的元素,他是蜘蛛爬取你的网站之 ...