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 ...
随机推荐
- myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为????
B 问题:myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为???? 解决:调整JSP页面编码:gb2312--->utf-8
- CUDA Samples: matrix multiplication(C = A * B)
以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...
- HDU 2323
http://acm.hdu.edu.cn/showproblem.php?pid=2323 把六边形抽象成坐标进行dp,抽象出的坐标关系必须满足六边形之间的关系.很有趣的一道dp #include ...
- CUDA 安装完成以后如何判断安装是否成功
最近在家里过寒假,可能这是还在学校里带着最大的福利了,无意之中翻出了多年前买的几本关于CUDA编程的书,于是随便在自己电脑上配置了一下环境,试试能不能把当年没有看完的书给看完了,于是有了今天这个判断C ...
- 未能加载文件或程序集“NPOI”或它的某一个依赖项
自己遇到过得一个很麻瓜很耽误时间的bug,也请教了一些大神嫩是没找到解决方法 下面分享下问题和解决方法 做的是一个下载功能,本地是没问题IIS站点导出EXCEL的时候出错 我这边看不到错误信息,只能一 ...
- 6-8 Percolate Up and Down(20 分)
Write the routines to do a "percolate up" and a "percolate down" in a binary min ...
- Linux function: unshare
When a new process is created with the clone() system call, a set of flags is provided which tells t ...
- Linux内核编译技巧
1.将多个文件编译成一个模块,部分文件可选 Example1: drivers/usb/core/Makefile:usbcore-y := usb.o hub.o hcd.o urb.o messa ...
- google chrome浏览器离线小恐龙游戏刷分bug
F12打开开发者工具->console->输入如下代码,分数要多少有多少 Runner.instance_.setSpeed(99999); 试试 瞬间 满分window.tempGame ...
- Shell编程时常用的系统文件(转)
10.1 Linux系统目录结构 / 根目录,所有文件的第一级目录 /home 普通用户家目录 /root 超级用户家目录 /usr 用户命令.应用程序等目录 /var 应用数据.日志等目录 /lib ...