【LeetCode】Expression Add Operators
来源:互联网 发布:linux怎么退出终端 编辑:程序博客网 时间:2024/06/09 16:13
题目:
Given a string that contains only digits 0-9
and a target value, return all possibilities to add binary operators (not unary) +
, -
, or *
between the digits so they evaluate to the target value.
Examples:
"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 -> []
思路:
首先这题要注意的是它不一定只能对一个数字做运算, 比如“123”, 23 -> ["1*23"] 也是正确的。
那么我们可以明显的看出来,这道题需要遍历这些数字的所有组合集合, 这时候最好用的就是递归方法。
假设我们的变量 curNum 代表当前从num 中读到的数字,lastAdd 是读到他之前最近的一次 加法(这里减法视为加法, 只是加了一个负数), curRst 为 读到curNum 之前所得的运算结果。于是在处理curNum时, 我们只需要考虑一下三种情况,加, 减, 乘,期中加减比较简单, 只需要继续对curRst进行累加并且更新lastAdd 就行(这里需要注意的是当进行减法的时候,lastAdd 更新时需要加一个负号)。 在做惩罚的时候, 我们首先需要将curRst - lastAdd, 然后再将lastAdd*curNum 加入(curRst - lastAdd)。 更简单的说明就是比如 1+2*3, 在我们便利到 3 想做乘法的时候, 我们的curRst 是1+2 = 3, lastAdd 是2, 我们应该香葱curRst(3) 中减去 lastAdd(2), 再将 2 * 3 加到1得到新的curRst 为6.
注意事项:base case 要写对。
代码:
public class Solution { public List<String> addOperators(String num, int target) { List<String> rst = new ArrayList<String>(); helper(rst, num, target, "", 0, 0); return rst; } private void helper(List<String> rst, String num, int target, String tmp, long curRst, long lastAdd){ //base case if(curRst == target && num.length() == 0){ rst.add(tmp); return; } //start at 1 for(int i = 1; i <= num.length(); i++){ String curStr = num.substring(0, i); //ignore numbers starting with 0, if curStr is long than 1 and the starting digit is 0, then return if(curStr.length() > 1 && curStr.charAt(0) == '0') return; String nxt = num.substring(i); long curNum= Long.parseLong(curStr); //tmp.length == 0 mean it is the first number, hence nothing need to be done if(tmp.length() == 0){ helper(rst, nxt, target, curStr, curNum, curNum); } else { //subtract, notice lastAdd is -curNum helper(rst, nxt, target, tmp+"-"+curNum, curRst - curNum, -curNum); //add helper(rst, nxt, target, tmp+"+"+curNum, curRst + curNum, curNum); //multiply, subtract the lastAdd from curRst, multiply lastAdd with curNum, update lastAdd to be lastAdd*curNum helper(rst, nxt, target, tmp+"*"+curNum, (curRst-lastAdd) + lastAdd*curNum, lastAdd*curNum); } } } }
阅读全文
0 0
- LeetCode:Expression Add Operators
- 【LeetCode】Expression Add Operators
- Expression Add Operators -- leetcode
- Leetcode -- Expression Add Operators
- LeetCode -- Expression Add Operators
- LeetCode Expression Add Operators
- leetcode:Expression Add Operators
- 《leetcode》:Expression Add Operators
- [leetCode] Expression Add Operators
- [LeetCode]Expression Add Operators
- [LeetCode] Expression Add Operators
- 【LeetCode】Expression Add Operators
- Expression Add Operators(leetcode)
- Leetcode | Expression Add Operators
- [leetcode 282]Expression Add Operators
- [leetcode] 282. Expression Add Operators
- [LeetCode]282. Expression Add Operators
- leetcode(282): Expression Add Operators
- 01背包问题和完全背包问题
- 日期格式和时间戳相互转换 外带一个获取当前程序根目录
- 文件格式之bmp
- Application server libraries not found && IntelliJ IDEA && tomcat
- (2017.06.26-2017.07.02)周总结-Java多线程/Mybatis批处理/数据库写覆盖
- 【LeetCode】Expression Add Operators
- 如何将自己的本地电脑变成可供外网访问的服务器
- Mybatis的xml文件执行sql返回多个值,java代码接收到一个值问题
- HDU
- Servlet规范核心类图
- ThreadLocal见解及知识备用
- B(1940): Remove the tree
- Java中byte与16进制字符串的互相转换
- linux—dup和dup2重定向文件描述符