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.

Example 1:

Input: num = "123", target = 6
Output: ["1+2+3", "1*2*3"]

Example 2:

Input: num = "232", target = 8
Output: ["2*3+2", "2+3*2"]

Example 3:

Input: num = "105", target = 5
Output: ["1*0+5","10-5"]

Example 4:

Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]

Example 5:

Input: num = "3456237490", target = 9191
Output: []

分析

解法是利用递归回溯来遍历所有的可能,但是要注意一些边界情形。

public class Solution {
public List<String> addOperators(String num, int target) {
List<String> rst = new ArrayList<String>();
if(num == null || num.length() == 0) return rst;
helper(rst, "", num, target, 0, 0, 0);
return rst;
}
  // eval记录当前计算结果,multed计算上次计算变化的部分,在选择乘法时会用到这个
public void helper(List<String> rst, String path, String num, int target, int pos, long eval, long multed){
if(pos == num.length()){
if(target == eval)
rst.add(path);
return;
}
for(int i = pos; i < num.length(); i++){
if(i != pos && num.charAt(pos) == '0') break;  // 抛弃以0开始的数字
long cur = Long.parseLong(num.substring(pos, i + 1));
if(pos == 0){
helper(rst, path + cur, num, target, i + 1, cur, cur); // 起始数字特殊处理
}
else{
helper(rst, path + "+" + cur, num, target, i + 1, eval + cur , cur);  // 对当前数字cur选择加上之前的部分
helper(rst, path + "-" + cur, num, target, i + 1, eval -cur, -cur);  // 选择减
        // 选择乘法要特殊处理,减去上次变化的部分,将这个变化的部分乘以当前的数字再加上去
helper(rst, path + "*" + cur, num, target, i + 1, eval - multed + multed * cur, multed * cur );
}
}
}
}

LeetCode282. Expression Add Operators的更多相关文章

  1. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  2. Expression Add Operators

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  3. LeetCode Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

  4. 282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  5. [leetcode]282. Expression Add Operators 表达式添加运算符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  6. 【LeetCode】282. Expression Add Operators

    题目: Given a string that contains only digits 0-9 and a target value, return all possibilities to add ...

  7. [LeetCode] 282. Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...

  8. LeetCode 282. Expression Add Operators

    原题链接在这里:https://leetcode.com/problems/expression-add-operators/ 题目: Given a string that contains onl ...

  9. [LeetCode] Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...

随机推荐

  1. supervisor "unix:///var/run/supervisor/supervisor.sock no such file" 解决方法

    如果是没有开启 supervisord 服务的情况下出现这种报错,可以先 systemctl start supervisor 试试, 如果不是,那就 sudo touch /var/run/supe ...

  2. bashttpd使用手册

    http://note.youdao.com/noteshare?id=15775dca9fcdc7326e80158082572ed5

  3. Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter)

    Java基础-IO流对象之转换流(InputStreamReader与OutoutStreamWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.转换流概述 我们之前 ...

  4. IOS使用mkdir创建目录

    在IOS真机上可以创建目录的位置只有两个Documents和Caches,如果直接在NSHomeDirectory()上创建目录,会失败,返回的errno含义为操作被禁止. 获取Caches中的一个目 ...

  5. 关于Python运行代码报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法

    运行python文件报错如上 解决办法: # -*- coding: UTF- -*- 或者 #coding=utf- (注:此语句一定要添加在源代码的第一行) 原因:Python默认是以ASCII作 ...

  6. Java并发编程原理与实战一:聊聊并发

    一.大纲 •你真的了解并发吗 •多线程和并发 •多线程和多进程 •线程一定快吗 •学习并发的四个阶段 •学习目标 •适合人群 •荐书   二.学习并发的四个阶段 •熟练掌握API,能够完成并发编程 • ...

  7. 安装rqalpha的日志

    安装rqalpha的日志 用anaconda的控制台命令: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple rqalpha rqalph ...

  8. Spring Data JPA原生SQL查询

    package com.test.cms.dao.repository;import org.springframework.stereotype.Repository;import javax.pe ...

  9. [转载]教你如何塑造JavaScript牛逼形象

    http://www.html5cn.org/article-6571-1.html 如何写JavaScript才能逼格更高呢?怎样才能组织JavaScript才能让别人一眼看出你不简单呢?是否很期待 ...

  10. just test css

    沃尔沃而 public void Commit() { if (_disposed) throw new InvalidOperationException(); if (_transaction = ...