1. 程式人生 > 實用技巧 >猿輔導前端面經

猿輔導前端面經

一面:

1、給定一組左閉右開的區間,如:

[1, 2), [3, 4), [4, 7), [6, 20)

輸出將連續區間合併後的結果,如:

[1, 2), [3, 20)

let merge = function(intervals) {
intervals.sort((a,b) => a.start-b.start)

for(let i=0;i<intervals.length-1;i++){
let interi1=intervals[i],interi2=intervals[i+1]

if(interi1.end >= interi2.start){
if(interi1.end < interi2.end){
interi1.end=interi2.end
}
intervals.splice(i+1,1)
i--
}
}
return intervals
};

2、有一個長度為 N + 1 的整數陣列,其中的元素取值範圍為 1…N(包含1和N),並且元素取值覆蓋 1…N(包含1和N)程式設計找到重複的數字

3、JS 的繼承

//組合繼承
function Parent(value){
this.val =value;
}
Parent.prototype.getValue = function(){
console.log(this.val);
}
function Child(){
Parent.call(this, value);
}
Child.prototype = new Parent();
const child = new Child(1);
//寄生組合繼承
Child.prototype = Object.create(Parent.prototype, {
constructor: {
value: Child,
enumerable: false,
writable: true,
configurable: true
}
})
//ES6繼承
class Parent {
constructor(value){
this.val = value;
}
getValue(){
console.log(this.val);
}
}
class Child extends Parent {
constructor(value){
super(value)
this.val = value;
}
}
let child = new Child(1);