java.lang.Object
g0201_0300.s0282_expression_add_operators.Solution

public class Solution extends Object
282 - Expression Add Operators.<p>Hard</p> <p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators</em> <code>'+'</code><em>,</em> <code>'-'</code><em>, and/or</em> <code>'*'</code> <em>between the digits of</em> <code>num</code> <em>so that the resultant expression evaluates to the</em> <code>target</code> <em>value</em>.</p> <p>Note that operands in the returned expressions <strong>should not</strong> contain leading zeros.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = &ldquo;123&rdquo;, target = 6</p> <p><strong>Output:</strong> [&ldquo;1*2*3&rdquo;,&ldquo;1+2+3&rdquo;]</p> <p><strong>Explanation:</strong> Both &ldquo;1*2*3&rdquo; and &ldquo;1+2+3&rdquo; evaluate to 6.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = &ldquo;232&rdquo;, target = 8</p> <p><strong>Output:</strong> [&ldquo;2*3+2&rdquo;,&ldquo;2+3*2&rdquo;]</p> <p><strong>Explanation:</strong> Both &ldquo;2*3+2&rdquo; and &ldquo;2+3*2&rdquo; evaluate to 8.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> num = &ldquo;105&rdquo;, target = 5</p> <p><strong>Output:</strong> [&ldquo;1*0+5&rdquo;,&ldquo;10-5&rdquo;]</p> <p><strong>Explanation:</strong></p> <pre><code> Both &quot;1*0+5&quot; and &quot;10-5&quot; evaluate to 5. Note that &quot;1-05&quot; is not a valid expression because the 5 has a leading zero. </code></pre> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> num = &ldquo;00&rdquo;, target = 0</p> <p><strong>Output:</strong> [&ldquo;0*0&rdquo;,&ldquo;0+0&rdquo;,&ldquo;0-0&rdquo;]</p> <p><strong>Explanation:</strong></p> <pre><code> &quot;0*0&quot;, &quot;0+0&quot;, and &quot;0-0&quot; all evaluate to 0. Note that &quot;00&quot; is not a valid expression because the 0 has a leading zero. </code></pre> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> num = &ldquo;3456237490&rdquo;, target = 9191</p> <p><strong>Output:</strong> []</p> <p><strong>Explanation:</strong> There are no expressions that can be created from &ldquo;3456237490&rdquo; to evaluate to 9191.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= num.length <= 10</code></li> <li><code>num</code> consists of only digits.</li> <li><code>-2<sup>31</sup> <= target <= 2<sup>31</sup> - 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details