← 返回 google 的题目列表Expression Add Operators (Hard) - Variant
类型:online_judge
Problem: Insert Operators in a Numeric String to Reach a Target (Variant)
You are given a string num consisting of digits only and an integer target. Insert operators between digits in num so that the resulting expression evaluates to target.
You may insert an operator between any two adjacent digits.
Every digit must be used exactly once and the order cannot be changed.
A number in the expression may contain multiple consecutive digits (i.e., you may concatenate digits).
Numbers in the expression must not have leading zeros (e.g., "05" is invalid, but "0" is valid).
Output
Return all expressions that evaluate to target (any order).
Notes
The expression contains at least one number.
Operator set and evaluation rules depend on the interviewer; if not specified, assume the common version with +, -, * and * has higher precedence than +/-.
Typical constraints
1 <= len(num) <= 10
-2^31 <= target <= 2^31-1
Examples
Example 1
Input: num = "123", target = 6
Output: ["1+2+3", "1*2*3"]
Example 2
Input: num = "105", target = 5
Output: ["1*0+5", "10-5"]
Example 3
Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]
Example
Input
123
6
Output
["1+2+3","1*2*3"]