面试算法题

面试算法题

  1. 给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度
/**
 * @param {string} s
 * @return {number}
 */
 var lengthOfLongestSubstring = function(s) {
    let arr = [], max = 0
    for(let i = 0;i < s.length; i++) {
        let index = arr.indexOf(s[i])
        if(index !== -1) {
            arr.splice(0, index+1);
        }
        arr.push(s.charAt(i))
        max = Math.max(arr.length, max) 
    }
    return max
};
  1. 实现一个方法用于比较两个版本号,满足以下要求
console.log(compareVersion('0.20.7', '0.20.8'));  // -1
console.log(compareVersion('0.20.9', '0.20.8'));  // 1
console.log(compareVersion('0.20.08', '0.20.8')); // 0
console.log(compareVersion('0.20.08', '0.20.8.1')); // -1
console.log(compareVersion('0.20.8.0', '0.20.8')); // 0
console.log(compareVersion('0.20.8.1', '0.20.8')); // 1
console.log(compareVersion('0.020', '0.20')); // 0

function compareVersion(v1, v2) {
  v1 = v1.split('.');
  v2 = v2.split('.');
  const len = Math.max(v1.length, v2.length);
  while (v1.length < len) {
    v1.push('0');
  }
  while (v2.length < len) {
    v2.push('0');
  }
  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i]);
    const num2 = parseInt(v2[i]);
    if (num1 > num2) {
      return 1;
    } else if (num1 < num2) {
      return -1;
    }
  }
  return 0;
}
  1. 请用 原型链 或者 class 实现一个计算器,能够实现链式加减乘除
    如:const calculator = new myCalculator(123);
    calculator.add (1). minus(2). multi(3).div(4).pow(2);
//使用原型链方法;
function myCalculator(num) {
  this.num = num;
}
myCalculator.prototype.add = function (n) {
  this.num = this.num + n;
  return this;
};
myCalculator.prototype.minus = function (n) {
  this.num = this.num - n;
  return this;
};
myCalculator.prototype.multi = function (n) {
  this.num = this.num * n;
  return this;
};
myCalculator.prototype.div = function (n) {
  this.num = this.num / n;
  return this;
};
let sum = new myCalculator(123);
sum.add(1).minus(4);
console.log(sum.num);

//使用class方法;
class MyCalculator {
  constructor(value) {
    this.value = value;
  }
  add(newValue) {
    this.value = this.value + newValue;
    return this;
  }
  reduction(newValue) {
    this.value = this.value - newValue;
    return this;
  }
  take(newValue) {
    this.value = this.value * newValue;
    return this;
  }
  division(newValue) {
    this.value = this.value / newValue;
    return this;
  }
}
let num = new MyCalculator(1);
let afterDate = num.add(2).reduction(2).take(5).division(2);
console.log(afterDate.value);
  1. 实现一个数组排序函数
    对数组[‘A华北’, ‘A华南’, ‘B华南’, ‘A华东’, ‘B华东’, ‘A华西’, ‘C华北’, ‘C华东’]
    排序为[‘A华东’, ‘A华南’, ‘A华西’, ‘A华北’, ‘B华东’, ‘B华南’, ‘C华东’, ‘C华北’]
    排序规则为相同字母A-C开头的A在前面,B、C次之;A相同的情况下按照东南西北的顺序再进行排序
arr = ['A华北', 'A华南', 'B华南', 'A华东', 'B华东', 'A华西', 'C华北', 'C华东']
function sort(arr) {
      const newArr = arr.sort((a, b) => {
        const ABCarr = ['A', 'B', 'C']
          const strArr = ['东', '南', '西', '北']
          const tmpa = a.slice(2)
          const tmpb = b.slice(2)
          return ABCarr.indexOf(a[0]) - ABCarr.indexOf(b[0]) || strArr.indexOf(tmpa[0]) - strArr.indexOf(tmpb[0]);
    });
      return newArr 
}
sort(arr)
  1. 请用js结合面向对象的思想,实现生产者和消费者的概念逻辑。
    //最好用下面这个实际例子:
    考虑一个在线的编辑器,用户会输入文字、插入图片、插入视频、插入分页等等,这些需要和后台交互,但不能阻塞用户的行为。
    注意,需要按顺序和后台交互。比如用户先插入一张图、再插入分页、再插入视频,需要在插入图片的后端成功返回后,再请求分页行为,然后请求视频的保存。
class Queue {
  providerList: any[];
  isLoop: boolean;
  constructor() {
    this.providerList = [];
    this.isLoop = false;
  }
  // 生产
  addTask(task: any) {
    this.providerList.push(task);
    // mock编辑操作
    // document.getElementById('input')?.appendChild(task.dom);
    if (!this.isLoop) {
      this.loop();
    }
  }

  // 消费
  consumeTask(task: any) {
    const myPromise = new Promise(function (resolve, reject) {
      //异步操作task传给后端
      const flag = true; // mock成功标志
      const result = { success: flag, result: flag ? 'success' : 'error' };
      if (result?.success) {
        resolve(result.result);
      } else {
        reject(result.result);
      }
    });
    return myPromise;
  }

  async loop() {
    this.isLoop = true;
    while (this.providerList.length) {
      const first = this.providerList[0];
      try {
        await this.consumeTask(first);
        this.providerList.shift();
      } catch (error) {
        console.log(error);
        // mock编辑操作
        //document.getElementById('input')?.removeChild(first.dom);
        this.providerList.shift();
      }
    }
    this.isLoop = false;
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值