算法题
129. 求根节点到叶节点数字之和
var sumNumbers = function(root) {var dfs = function(root, path) {if (root == null) return 0path = path * 10 + root.valif (!root.left && !root.right) return pathreturn dfs(root.left, path) + dfs(root.right, path)}return dfs(root, 0)};
思路:
首先是标准的bfs递归,bfs中间的步骤是路径的叠加。
手写题
给定只含有a、b 和 c的字符串,请去掉其中的b 和 ac。
removeChars('ab') // 'a'removeChars('abc') // ''removeChars('cabbaabcca') // 'caa'
function removeChars(input) {input = input.split('')// your code herefor(let i = 0; i<input.length;i++) {if (input[i] === 'b') {input.splice(i, 1)i--}}let left = 0let idx = 0while(true) {if (input[left] + input[left + 1] === 'ac') {idx = 1input.splice(left, 2)}left++if (left >= input.length - 1) {if (idx === 1) {left = 0idx = 0} else {return input.join('')}}}}
