LeetCode 282: Expression Add Operation

Note:

1. When real index is 0. It will be only add as first element without any opeartors.

2. When the starting char of one layer is ‘0‘, it should break since it could not be 01, 02, 03......

class Solution {
    public List<String> addOperators(String num, int target) {
        List<String> result = new ArrayList<>();
        generateOperators(result, num, "", 0, 0, target, 0L);
        return result;
    }

    private void generateOperators(List<String> result, String num, String current, int index, long value, int target, long multi) {
        if (index == num.length()) {
            if (value == target) {
                result.add(current);
            }
            return;
        }

        for (int i = index; i < num.length(); i++) {
            if (i != index && num.charAt(index) == ‘0‘) {
                break;
            }

            long data = Long.parseLong(num.substring(index, i + 1));

            if (index == 0) {
                generateOperators(result, num, current + num.substring(index, i + 1), i + 1, value + data, target, data);
            } else {
                generateOperators(result, num, current + "+" + num.substring(index, i + 1), i + 1, value + data, target, data);
                generateOperators(result, num, current + "-" + num.substring(index, i + 1), i + 1, value - data, target, -data);
                generateOperators(result, num, current + "*" + num.substring(index, i + 1), i + 1, value - multi + multi * data, target, multi * data);
            }
        }
    }
}
时间: 2024-08-29 09:26:24

LeetCode 282: Expression Add Operation的相关文章

[LeetCode][JavaScript]Expression Add Operators

Expression Add Operators 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 -> [

【LeetCode】282. Expression Add Operators

题目: 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", &q

282 Expression Add Operators 给表达式添加运算符

给定一个仅包含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"]&

282. Expression Add Operators

解题思路:深搜 遍历所有可能的情况,终止条件为表达式计算的结果等于target.注意,这里要使用long 类型保存整数,否则会溢出.我在vs2013,win32模式下使用long同样不能通过 "3456237490", 9191 -> [] 这个案例,改用long long 才可以. 具体解析可以参考文末的这篇文章,讲的很好,主要要注意乘法情况下如何计算表达式的值. class Solution { public: void compute(vector<string>

[LeetCode] Expression Add Operators

This post shares a very nice solution, which is rewritten below. 1 class Solution { 2 public: 3 vector<string> addOperators(string num, int target) { 4 int n = num.length(); 5 if (!n) return {}; 6 vector<string> ans; 7 for (int i = 1; i <=

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

Given a string that contains only digits 0-9 and a target value, return all possibilities to add operators +, -, or * between the digits so they evaluate to the target value. Examples: "123", 6 -> ["1+2+3", "1*2*3"] "

Leetcode: Ternary Expression Parser

Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. You can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F (T and F represent True and False resp

LeetCode OJ #2 Add Two Numbers

https://leetcode.com/problems/add-two-numbers/ 题意:两个链表表示两个数 , 2 4 3 5 6 4 按照加法的规则从左往右加以及 进位. 输出:和的链表. 仍然是水题.一些特殊情况需要考虑(这些做题的时候我考虑到了) 1.如果两个长度不等,如l1>l2  ,那么需要l1比l2多出来的部分加上进位copy到ans里 2.5+5的情况,虽然长度相等,但是最高位进位了 可惜,WA了两发,因为 正确的应该是 int v = l1->val + l2-&g

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(