282 expression and operations添加运算符
[抄题]:
给定一个仅包含数字 0 - 9 的字符串和一个目标值,返回在数字之间添加了 二元 运算符(不是一元)+, - 或 * 之后所有能得到目标值的情况。
"123", 6 -> ["1+2+3", "1*2*3"]
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 不知道怎么插缝:单独命名一个变量pos记录位数信息。这个还是要加上的,以前没用过
- 感觉要用012控制+_*,想得太麻烦了,没必要增加变量,直接分情况写3个表达式就行了
- if (i != pos && num.charAt(pos) == '0') { break; } 02,0233431这些情况需要break。此题特殊
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

本来是加上lastf的,变成乘法了,把原来加的减去即可
分别用函数中的当前改变量、当前结果来表示,体现乘法时的特殊操作,改变量需要正负分明。改变量和结果不对应的时候,可以用两个变量来表示。
[一刷]:
- 字符串的corner case中,没有实例化对象是null,有对象但为空值是length() = 0,两种情况要分开,以前不理解。
- 当eval == target就符合题目的要求 可以退出了,没理解题目的目的。看清数据结构,不要瞎加对象。
- dfs中包括的dfs,利用其中的参数改变来进行recursion
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(3^所有数字个数) Space complexity: O(3*所有数字个数)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
枚举型dfs, for循环出所有方法
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
224. Basic Calculator
150. Evaluate Reverse Polish Notation 用stack控制进出
241. Different Ways to Add Parentheses分治型(为啥不是枚举型?)DFS方法
494. Target Sum DP所有方法、DFS所有方法
[代码风格] :
public class Solution {
/*
* @param num: a string contains only digits 0-9
* @param target: An integer
* @return: return all possibilities
*/
public List<String> addOperators(String num, int target) {
//corner case
List<String> results = new ArrayList<>();
if (num.length() == 0 || num == null) {
return results;
}
dfs(num, "", results, target, 0, 0, 0);
return results;
}
//dfs
//corner case
private void dfs(String num, String path, List<String> results,
int target, int pos, long eval, long multed) {
//reach end
if (pos == num.length()) {
if (eval == target) {
results.add(path);
}
return;
}
//02,098
for (int i = pos; i < num.length(); i++) {
if (i != pos && num.charAt(pos) == '0') {
break;
}
long cur = Long.parseLong(num.substring(pos, i + 1));
if (pos == 0) {
dfs(num, path + cur, results, target, i + 1, cur, cur);
}else {
dfs(num, path + "+"+ cur, results, target, i + 1, eval + cur, cur);
dfs(num, path + "-"+ cur, results, target, i + 1, eval - cur, -cur);
dfs(num, path + "*"+ cur, results, target, i + 1, eval - multed + multed * cur, multed * cur);
}
}
}
}
282 expression and operations添加运算符的更多相关文章
- 282 Expression Add Operators 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...
- Java实现 LeetCode 282 给表达式添加运算符
282. 给表达式添加运算符 给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+.- 或 * ,返回所有能够得到目标值的表达式. 示例 1: 输入: num = ...
- [leetcode]282. Expression Add Operators 表达式添加运算符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- leetcode 282. 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况. 例如: "123", 6 -> [" ...
- 282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
- [LeetCode] 282. Expression Add Operators 表达式增加操作符
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- LeetCode 282. Expression Add Operators
原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...
- 【LeetCode】282. Expression Add Operators
题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...
随机推荐
- MongoDB数据库的特点以及结构
mongodb标签:非关系型数据库 文档型数据库 最像关系型的非关系型数据库 特点: 1. 由c++编写的数据库管理系统 2. 支持丰富的增删改查功能 3. 支持丰富的 ...
- 掉电脉冲映射串口log和dmesg到文件中的log
1.echo 1 > /mytest/boot_times 2.systemctl enable i2c_dmesg.service root:/mytest# tree . |-- boot_ ...
- Spring4源码解析:BeanDefinition架构及实现
一.架构图 首先共同看下总体的 Java Class Diagrams 图: 二.具体类实现 2.1 AttributeAccessor 接口定义了一个通用的可对任意对象获取.修改等操作元数据的附加契 ...
- macdown在mac OS 中的配置
macdown 用命令行打开.md文件 执行两条命令即可. sudo echo "open -a MacDown \$*" > /usr/local/bin/macdown ...
- laravel 控制器中使用 try catch
需要操作数据库时,当数据字段不一致,mysql报错,控制程序,需要使用try catch 下面是使用案例 $morder['morder_time'] = time();//在这里使用try catc ...
- 租用游艇(简单区间dp)
租用游艇 时间限制: 1 Sec 内存限制: 128 MB提交: 1 解决: 1[提交][状态][讨论版][命题人:quanxing] 题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2 ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- Hive 体系结构
1.Hive架构与基本组成 下面是Hive的架构图. 图1.1 Hive体系结构 Hive的体系结构可以分为以下几部分: (1)用户接口主要有三个:CLI,Client 和 W ...
- sqlserver基础操作
启动服务: 1.在系统服务启动 2.在sql配置管理器服务选项中启动 3.在管理员cmd:net start mssqlserver;net stop mssqlserver use master g ...
- 基于DB的编程
现在我们大多数的开发都是基于数据库,虽然数据库为我们提供了事务机制,保证存储的数据的ACID,但是,当我们在完成一个业务操作时,涉及到对数据库的大量操作,如果把这些操作在一个事务中,肯定是安全的,但是 ...