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 ...
随机推荐
- JavaScript学习(一)——基础知识查漏补缺
标签script 我们知道,html要使用js就要使用<script>标签. 两种方式: 一是直接在<script>这里</script>写入代码. 二是在别的文件 ...
- 微信小程序页面跳转的四种方法
wx.navigateTo({}) ,保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 可以返回; 示例: 1 wx.navigateTo({ 2 url:'../test/ ...
- 6-18 Two Stacks In One Array(20 分)
Write routines to implement two stacks using only one array. Your stack routines should not declare ...
- Python 字典(Dictionary) update()方法
refer to: http://www.runoob.com/python/att-dictionary-update.html
- fusionjs 学习一 基本试用
参考demo 项目 https://github.com/rongfengliang/fusionjs-docker-demo 安装 create startkit yarn global add c ...
- 写时复制和fork,vfork,clone
写时复制 原理: 用了“引用计数”,会有一个变量用于保存引用的数量.当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时, ...
- ASP.NET 获得当前网页名字
Code string currentFilePath = HttpContext.Current.Request.FilePath; string CurrentPageName = current ...
- ipython的使用
改初始路径 还有一个坑,可以用notebook打开一个已经存在的文件,但是不能正常编辑(使用单元编辑),因为使用这个创建的东西根本就不是一个.py文件,如果代码编辑完毕,倒是可以通过下载那里选择下载成 ...
- flask 蓝图
转自:http://spacewander.github.io/explore-flask-zh/7-blueprints.html 蓝图 什么是蓝图? 一个蓝图定义了可用于单个应用的视图,模板,静态 ...
- android.support.v7.internal.widget.ActionBarOverlayLayout Couldn't Be Initialized
问题症状: Android Studio 1.2 (Build 141.1890965) 新建工程,自动build完成后,Layout Editor无法预览Layout文件,报错内容: Renderi ...