zoukankan      html  css  js  c++  java
  • 前端基础知识学习第六节(数据结构篇)

    1.

      如何判断一个单向链表是否有环?

      答案:
      1)创建哈希表。遍历链表,将链表各节点添加至哈希表中,添加前判断此节点是否已存在哈希表中,存在的话说明链表中存在环
      2)给节点添加visited访问标记。遍历链表,每访问一个新节点,使其visited为1,每次访问节点前先判断其visited是否为1,为1则是已访问过的节点,
      说明链表中存在环
      3)快慢指针法,设定快指针fast,慢指针slow,每次循环快指针fast移动两个位置,慢指针slow移动一个位置,如果快指针和慢指针在某一个节点相遇
      则说明链表中有环
      参考:https://segmentfault.com/a/1190000018439965

    2.

      使用递归和非递归实现二叉树的前序遍历?

      答案:
      let TreeNode = {
        val: 1,
        left: {
          val: 2,
          left: {
            val: 4
          },
          right: {
            val: 5
          }

        },
        right: {
          val: 3,
          left: {
            val: 6
          },
          right: {
            val: 7
          }
        }
      }
      // 递归方式
      function preOrderRecur(root) {
        let list = [];

        function preOrder(root) {
          if (!root) {
            return root;
          }

          list.push(root.val);
          
          preOrder(root.left);
          preOrder(root.right);
        }

        preOrder(root);

        return list;
      }
      // 非递归方式
      function preOrder(root) {
        let list = [];
        let stack = [ root ];

        while (stack.length) {
          const cur = stack.pop();
          const right = cur.right;
          const left = cur.left;

          list.push(cur.val);

          if (right) {
            stack.push(right);
          }

          if (left) {
            stack.push(left);
          }
        }

        return list;
      }

  • 相关阅读:
    br_
    -fpic -fPIC -fpie -fPIE
    (OK) cross_compile_readline_for_android-x86_64-6.0
    Installing the GNU Readline library
    How to get libreadline for Android?
    (OK)(OK) seem-tools-init-android-x86_64-6.0-rc1-0.sh
    (OK)(OK) seem-tools-CLI-semi-auto.sh---nic1_2-hostonly-bridged
    (OK)(OK) seem-tools-auto_create_vm_android.sh
    (OK) run my script at boot time in android-x86_64 (Chih-Wei Huang)
    【习题 3-4 UVA
  • 原文地址:https://www.cnblogs.com/typeof/p/12271714.html
Copyright © 2011-2022 走看看