当前位置:网站首页>leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
2022-07-07 12:56:00 【InfoQ】
一、题目大意
- 1 <= expression.length <= 20
- expression 由数字和算符 '+'、'-' 和 '*' 组成。
- 输入表达式中的所有整数值在范围 [0, 99]
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public List<Integer> diffWaysToCompute(String expression) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < expression.length(); i++) {
char ope = expression.charAt(i);
if (ope == '+' || ope == '-' || ope == '*') {
List<Integer> left = diffWaysToCompute(expression.substring(0, i));
List<Integer> right = diffWaysToCompute(expression.substring(i + 1));
for (int l : left) {
for (int r : right) {
switch (ope) {
case '+':
ans.add(l + r);
break;
case '-':
ans.add(l - r);
break;
case '*':
ans.add(l * r);
break;
}
}
}
}
}
if (ans.isEmpty()) {
ans.add(Integer.valueOf(expression));
}
return ans;
}
}
四、总结小记
- 2022/7/7 好事多磨,又要延期一周
边栏推荐
- 缓冲区溢出保护
- AWS learning notes (III)
- Integer learning
- What is the process of ⼀ objects from loading into JVM to being cleared by GC?
- Classification of regression tests
- 智汀不用Home Assistant让小米智能家居接入HomeKit
- Used by Jetson AgX Orin canfd
- CTFshow,信息搜集:web9
- asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
- Instructions d'utilisation de la trousse de développement du module d'acquisition d'accord du testeur mictr01
猜你喜欢

Niuke real problem programming - day14

“百度杯”CTF比赛 2017 二月场,Web:include

数学建模——什么是数学建模

电脑Win7系统桌面图标太大怎么调小

Zhiting doesn't use home assistant to connect Xiaomi smart home to homekit

Today's sleep quality record 78 points

防火墙基础之服务器区的防护策略

"July 2022" Wukong editor update record

Introduction and use of Kitti dataset

安恒堡垒机如何启用Radius双因素/双因子(2FA)身份认证
随机推荐
What is cloud primordial? This time, I can finally understand!
智汀不用Home Assistant让小米智能家居接入HomeKit
在软件工程领域,搞科研的这十年!
Discussion on CPU and chiplet Technology
Lidar knowledge drops
Base64 encoding
Instructions for mictr01 tester vibrating string acquisition module development kit
CTFshow,信息搜集:web10
asp. Netnba information management system VS development SQLSERVER database web structure c programming computer web page source code project detailed design
Cocoscreator resource encryption and decryption
Delete a whole page in word
Stm32cubemx, 68 sets of components, following 10 open source protocols
WebRTC 音频抗弱网技术(上)
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
Attribute keywords serveronly, sqlcolumnnumber, sqlcomputecode, sqlcomputed
Niuke real problem programming - day15
「2022年7月」WuKong编辑器更版记录
一文读懂数仓中的pg_stat
AWS学习笔记(三)
buffer overflow protection