Expression Add Operators(dfs)
Given a string that contains only digits0-9and a target value, return all possibilities to addbinaryoperators (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:
Example 4:
Example 5:
分析
Complexity:
O(4^n)
dfs 因为中间数字可以任何结合成为新数,start做dfs参数,end由for loop决定
这里pos,path都有,多了个prevn记录之前得数,是为了*模仿栈
注意这里start end都包含在内
Last updated
Was this helpful?