Expression Add Operators LeetCode solution in JavaScript
1var addOperators = function (num, target) {2let res = [];34let i = 0;56const helper = (idx, sum, prev, path) => {7if (idx >= num.length) {8if (sum === target) {9res.push(path);10}11return null;12}1314for (let j = idx; j < num.length; j++) {15if (j !== idx && num[idx] === "0") break;1617let n = Number(num.slice(idx, j + 1));1819if (idx === 0) {20helper(j + 1, sum + n, sum + n, path + n);21} else {22helper(j + 1, sum + n, n, path + "+" + n);2324helper(j + 1, sum - n, 0 - n, path + "-" + n);2526helper(j + 1, sum - prev + prev * n, prev * n, path + "*" + n);27}28}29};3031helper(i, 0, 0, "");3233return res;34};