Skip to content

美团暑期笔试

选择题

  1. 以下代码会输出 'zhoumowan',因为 this 指向 person 对象,同时可以通过调用方法改变 this 指向。判断这句话的正确性?

    1.js
    js
    const person = {
      name: "zhoumowan",
      sayName: function () {
        console.log(this.name);
      },
    };
    person.sayName();
  2. 选择正确的访问属性 a 的方法。

    2.js
    js
    const myName = Symbol();
    obj = {
      [myName]: "123",
    };
  3. 选择正确的输出。

    3.js
    js
    var a = 1;
    function fn1(b) {
      var a = 2;
      return fn2(b);
    }
    function fn2(b) {
      return a + b;
    }
    const res = fn1(3);
    console.log(res);
  4. 判断三个盒子各自的宽度。

    HTML
    <!doctype html>
    <html lang="en">
    <head>
       <meta charset="UTF-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <title>Document</title>
       <style>
          .wrapper1 {
          width: 100px;
          height: 100px;
          }
          .wrapper2 {
          width: 50px;
          height: 50px;
          }
          .two {
          float: left;
          }
          .three {
          position: relative;
          }
       </style>
    </head>
    <body>
       <div class="wrapper1">
          <div class="one"></div>
          <div class="two"></div>
       </div>
       <div class="wrapper2">
          <div class="three"></div>
       </div>
    </body>
    </html>

编程题

  1. 给定一个字符串,并给定一个所有有垂直对称轴的字符集,求出字符串中所有垂直对称的回文子串的个数。

    4.js
    js
    const yes = ["A", "H", "I", "M", "O", "T", "U", "V", "W", "X", "Y"];
    
    function solution(str) {
      const length = str.length;
      let result = 0;
      for (let i = 0; i < length; i++) {
        for (let right = i; right < length; right++) {
          const cur = str.slice(i, right + 1);
          if (valid(cur) && cur.length > 1) result++;
        }
      }
      return result;
    }
    
    function valid(str) {
      let left = 0;
      let right = str.length - 1;
      while (left <= right) {
        if (!yes.includes(str[left]) || !yes.includes(str[right])) return false;
        if (str[left] !== str[right]) return false;
        left++;
        right--;
      }
      return true;
    }
    const result = solution("AAAA");
    console.log(result); // 6
  2. 输入一个询问次数,接下来每输入两行,第一行表示数组长度,第二行是数组内容,求有多少个子区间数组能构成好数组。好数组的定义是:数组长度为奇数的同时,不经排序就实现了中位数位置正确。

    5.js
    js
    const rl = require("readline").createInterface({
      input: process.stdin,
    });
    let times = 0;
    rl.on("line", (line) => {
      inputs.push(line);
      times = +inputs[0];
      if (inputs.length === times * 2 + 1) rl.close();
    });
    rl.on("close", () => {
      for (let i = 1; i <= times; i++) {
        const length = +inputs[2 * i - 1];
        const array = inputs[2 * i].split(" ").map(Number);
        console.log(solution(array));
      }
    });
    
    /**
     *
     * @param {Array<Number>} array
     * @returns {Number}
     */
    function solution(array) {
      let result = 0;
      const length = array.length;
      const sli = array.slice();
      array.sort((a, b) => a - b);
      for (let left = 0; left < length; left++) {
        let right = left;
        while (left <= right && right < length) {
          const curSli = sli.slice(left, right + 1);
          const curArr = array.slice(left, right + 1);
          if (valid(curSli, curArr)) {
            result++;
          }
          right += 2;
        }
      }
      return result;
    }
    
    function valid(cur1, cur2) {
      const length = cur1.length;
      return length === 1 || cur1[(length - 1) / 2] === cur2[(length - 1) / 2];
    }