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 ...
随机推荐
- IIS经典模式与集成模式
在IIS7.0中Web应用程序有两种配置形式:经典和集成 经典模式 经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库,原先运行于IIS6.0下的Web应用程序迁移到IIS7 ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- Convex 一道阿姆斯特朗回旋好题
2001年5月8日,阿姆斯特朗(Armstrong, 1929-2013) 教授发明了一种名为“阿姆斯特朗回旋加速喷气式阿姆斯特朗加密”的加密算法,算法从未公开,直至2013阿姆斯特朗教授逝世后,其生 ...
- ssh框架中spring整合hibernate的配置文件模板(带详细注释)
applicationContext.xml的配置文件模板 <?xml version="1.0" encoding="UTF-8"?> <b ...
- sourcegraph 方便的代码查看工具
sourcegraph 是一个方便的代码查看插件,有chrome 的插件,具体安装可以在chrome 应用商店,同时 官方提供了基于docker 运行的方式(适合本地使用) 下载镜像 docker p ...
- 关于Eclipse中import javax.servlet.*出错
今天为了调试一下我写的Servlet,突然间,发现我的站点下所有的Servlet全部都出错了,仔细一看,原来是import javax.servlet.*这里出错了. 然后我就上网查阅了一些资料,才发 ...
- python删除x天前文件及文件夹
#!/usr/bin/env python # -*- coding:utf-8 -*- import os, time, sys, shutil def delFiles(beforeSec, di ...
- 移植SDL最新版本(转)
原文出自:http://blog.csdn.net/flyyang123456789/article/details/17223485 首先 将所要移植的包准备好 有 SDL2-2.0.1.tar. ...
- 02 - Unit08:搜索笔记功能、搜索分页、处理插入数据库乱码问题
搜索笔记功能 按键监听事件 $("#search_note").keydown(function(event){ var code=event.keyCode; if(code== ...
- configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
编译php出现错误: configure: error: Please reinstall the libcurl distribution - easy.h should be in <cur ...