2020春招美团笔试题

写在前面


楼主大三,春招实习。总结一下笔试的错题。
有错误望指正,感激不尽。

问答题


this指针的绑定

var name = 'global';
var obj = {
    name: 'local',
    foo: function(){
        this.name = 'foo';
    }.bind(window)
};
var bar = new obj.foo();
setTimeout(function() {
    console.log(window.name);
}, 0);
console.log(bar.name);
  
var bar3 = bar2 = bar;
bar2.name = 'foo2';
console.log(bar3.name);

结果:‘foo’,‘foo2’.‘global’

四种this的指向

ES6编译成ES5

将如下ES6代码编译成ES5
ES6

class Person {
     constructor (name) {
          this.name = name;
     }
     greet () {
          console.log(`Hi, my name is ${this.name}`);
     }
     greetDelay (time) {
          setTimeout(() => {
               console.log(`Hi, my name is ${this.name}`);
          }, time);
     }
}

ES5

var Person = (function(){
    this.name = name 
    Person.prototype.greet = function(){
            console.log("Hi, my name is" + this.name)
        }    
    Person.prototype.greetDelay = function(time){
            var that = this
            setTimeout(function(){
                 cosole.log("Hi, my name is" + that.name)
            },time)
        }
})()

算法


美团的算法貌似很重视动态规划,不过都是leetcode的类似题改编的。

斐波那契数列

美团的斐波那契数列使用暴力递归会造成栈的移除,所以简化成下面的写法。

形如1, 1, 2, 3, 5, 8, 13, 21, 34, 55的数列,后一位是前面两位相加(斐波那契数列),写出函数要求找到第 N位是多少,如:fib(3) => 3 , fib(5) => 8, 要求时间复杂度为O(n)。

let num = readline()
function fabonacci(num){
    if(num === 0 ||num === 1 ) return 1
    let res = [1,1,2]
    for(let i = 3 ;i<=num ;i++){
        res[i] = res[i-1] + res[i-2]
    }
    return res[num]
 }
console.log(fabonacci(num))

最少硬币问题

美团的第二题是动态规划问题,动态规划的核心在于将整体问题分解成局部问题进行求解。根据题意找到动态方程dp[i] = Math.mix(dp[i-coin] +1,dp[i])
零钱兑换

近期某商场由于周年庆,开启了“0元购”活动。活动中,消费者可以通过组合手中的代金券,实现0元购买指定商品。
聪明的小团想要用算法来帮助他快速计算:对于指定价格的商品,使用代金券凑出其价格即可,但所使用的代金券总面额不可超过商品价格。由于代金券数量有限,使用较少的代金券张数则可以实现价值最大化,即最佳优惠。
假设现有100元的商品,而代金券有50元、30元、20元、5元四种,则最佳优惠是两张50元面额的代金券;而如果现有65元的商品,则最佳优惠是两张30元代金券以及一张5元代金券。
请你帮助小团使用一段代码来实现代金券计算。

while(true){
    let nums = parseInt(readline())
    if(nums === 0 ) break
    let coins = readline().split(" ").map(Number).slice(1)
    console.log(fn(coins,nums))
}
function fn(coins,nums){
    let dp = new Array(nums+1).fill(Infinity)
    dp[0] = 0
    for(let i = 1;i<=nums;i++){
        for(let coin of coins){
            if( i >= coin ){
                dp[i] = Math.min(dp[i],dp[i-coin]+1)
            }
        }
    }
    return dp[nums] === Infinity ? 'Impossible' : dp[nums]
}

最小路径和

美团的第三题依旧是动态规划问题。
关键还是动态方程dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j]
leetcode原题

给定一个包含非负整数的 M x N 迷宫,请找出一条从左上角到右下角的路径,使得路径上的数字总和最小。每次只能向下或者向右移动一步。

let array = readline().split(" ").map(Number)
let row = array[0],col = array[1]
let grid = []
while(line = readline()){
    grid.push(line.split(" ").map(Number))
}
function fn(row,col,grid){
    let martix = Array.from(new Array(row),()=>new Array(col))
    for(let i = 0;i < row;i++){
        for(let j = 0;j < col;j++){
            if(i === 0 && j ===0 ){
                martix[i][j] = grid[i][j]
            }else if(i === 0 && j > 0){
                martix[i][j] = martix[i][j-1] + grid[i][j]
            }else if(i > 0 && j === 0){
                martix[i][j] = martix[i-1][j] + grid[i][j]
            }else{
                martix[i][j] = Math.min(martix[i][j-1],martix[i-1][j])
                                        + grid[i][j]
            }
        }
    }
    return martix[row-1][col-1]
}
console.log(fn(row,col,grid))
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值