一、手写算法

https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/

思路


  • 代码

    ```javascript /**
    • Definition for a binary tree node.
    • function TreeNode(val, left, right) {
    • this.val = (val===undefined ? 0 : val)
    • this.left = (left===undefined ? null : left)
    • this.right = (right===undefined ? null : right)
    • } / /*
    • @param {TreeNode} root
    • @return {number} */ const dfs = (root, prevSum) => { if (root === null) {
      1. return 0;
      } const sum = prevSum * 10 + root.val; if (root.left == null && root.right == null) {
      1. return sum;
      } else {
      1. return dfs(root.left, sum) + dfs(root.right, sum);
      } }

var sumNumbers = function(root) { return dfs(root, 0); };

  1. <a name="fbzED"></a>
  2. ### 复杂度分析
  3. - 时间复杂度:
  4. - 空间复杂度:
  5. <a name="Ghcnm"></a>
  6. ## 二、编程题
  7. ```javascript
  8. // https://bigfrontend.dev/zh/problem/remove-characters
/**
 * 解法1
 * @param {string} input
 * @returns string
 */
function removeChars(input) {
  // your code here
    var len = input.length;
  var stack = [];
  var i = 0;
  while (i < len) {
    if (input[i] === 'c') {
      if (stack.length > 0) {
        var temp = stack[stack.length - 1];
        temp === 'a' ? stack.pop() : stack.push(input[i]);
      } else {
        stack.push(input[i]);
      }
    }
    if (input[i] === 'a') {
      stack.push(input[i]);
    }
    i++;
  }
  return stack.join('');
}

/**
 * 解法2
 * @param {string} input
 * @returns string
 */
function removeChars(input) { 
   const regExp = /b|(ac)/g;
   while (input.match(regExp)) {
     input = input.replace(regExp, '');
   }
   return input;
}