面试算法题
- 给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度
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
};
- 实现一个方法用于比较两个版本号,满足以下要求
console.log(compareVersion('0.20.7', '0.20.8'));
console.log(compareVersion('0.20.9', '0.20.8'));
console.log(compareVersion('0.20.08', '0.20.8'));
console.log(compareVersion('0.20.08', '0.20.8.1'));
console.log(compareVersion('0.20.8.0', '0.20.8'));
console.log(compareVersion('0.20.8.1', '0.20.8'));
console.log(compareVersion('0.020', '0.20'));
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;
}
- 请用 原型链 或者 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 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);
- 实现一个数组排序函数
对数组[‘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)
- 请用js结合面向对象的思想,实现生产者和消费者的概念逻辑。
//最好用下面这个实际例子:
考虑一个在线的编辑器,用户会输入文字、插入图片、插入视频、插入分页等等,这些需要和后台交互,但不能阻塞用户的行为。
注意,需要按顺序和后台交互。比如用户先插入一张图、再插入分页、再插入视频,需要在插入图片的后端成功返回后,再请求分页行为,然后请求视频的保存。
class Queue {
providerList: any[];
isLoop: boolean;
constructor() {
this.providerList = [];
this.isLoop = false;
}
addTask(task: any) {
this.providerList.push(task);
if (!this.isLoop) {
this.loop();
}
}
consumeTask(task: any) {
const myPromise = new Promise(function (resolve, reject) {
const flag = true;
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);
this.providerList.shift();
}
}
this.isLoop = false;
}
}