算法题:

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

    1. function dfs(root,num) {
    2. if (!root) {
    3. return 0
    4. }
    5. let sum = 10*num+root.val
    6. if (!root.left && !root.right) {
    7. return sum
    8. } else {
    9. return dfs(root.left,sum)+dfs(root.right,sum)
    10. }
    11. }
    12. var sumNumbers = function(root) {
    13. return dfs(root,0)
    14. };

    手写题:

  • https://bigfrontend.dev/zh/problem/remove-characters

  • 题目:给定只含有a、b 和 c的字符串,请去掉其中的b 和 ac。

    1. removeChars('ab') // 'a'
    2. removeChars('abc') // ''
    3. removeChars('cabbaabcca') // 'caa'
  • 解题: ```javascript function removeChars(input) { // your code here let str = ‘’ for (let i =0,len=input.length;i<len;i++){ if (input[i] ===’c’) {

    1. if (str[str.length-1] ==='a' && str[str.length-1]) {
    2. str = str.slice(0,str.length-1)
    3. } else {
    4. str=str+input[i]
    5. }

    } if (input[i] === ‘a’){

    1. str=str+input[i]

    } } return str }

```