Skip to content

Instantly share code, notes, and snippets.

@fxxkscript
Last active May 22, 2020 12:03
Show Gist options
  • Save fxxkscript/003eafc693725f45e11d4701ddcb386f to your computer and use it in GitHub Desktop.
Save fxxkscript/003eafc693725f45e11d4701ddcb386f to your computer and use it in GitHub Desktop.
面试

做题时间一小时

第一题

O(n^2)

2层For 循环

第二题

一个字节byte = 8 比特bit

一个字符character有一个或者多个byte字节组成

字符串string 由多个字符character组成

Unicode是字符集,为每一个字符分配一个唯一的 ID

UTF-8, UTF-16, GB2312, GB18030 这些是不同的编码规则

UTF-8 一个字符最少8个bit

UTF-16 一个字符最少16个bit

GB2312, GB18030是中国的标准

第三题

iOS / Mac

VSCode, Chrome, WeChat

第四题

构建器与工厂模式的主要区别在于:当创建对象是一步过程时,使用工厂模式,而当创建是多步过程复杂时,将使用构建器模式。

Adapter适配器模式,包装不兼容的对象使得满足其他类的要求

Decorator装饰器模式,通过包装一个装饰器的类去动态改变对象运行时的行为

// Factory
class House1 {
  width: number;
  height: number;
  constructor(width, height) {
    this.width = width;
    this.height = height;
  }

  paint() {}
}

class HouseFactory {
  static makeHouse(width, height) {
    let house = new House1(width, height);
    house.paint();

    return house;
  }
}

HouseFactory.makeHouse(100, 100);

// Builder
class House {
  width: number;
  height: number;
  color: string;
  car: number;
  door: number;
  constructor(width: number, height: number, color: string, car: number, door: number) {
    this.width = width;
    this.height = height;
    this.color = color;
    this.car = car;
    this.door = door;
  }
}

class HouseBuilder {
  width: number;
  height: number;
  color: string;
  car: number;
  door: number;
  constructor(width: number, height: number) {
    this.width = width;
    this.height = height;
    this.color = 'blue';
    this.door = 1;
    this.car = 0;
  }

  setColor(color: string) {
    this.color = color;
    return this;
  }

  addCar() {
    this.car++;
    return this;
  }

  addDoor() {
    this.door++;
    return this;
  }

  build() {
    return new House(this.width, this.height, this.color, this.car, this.door);
  }
}

new HouseBuilder(100, 100).addCar().setColor('red').build();


// adapter
interface IOSAPP {
    show(name: string): boolean;
}

class IOS {
    run(app: IOSAPP) {

    }
}

class AndroidApp {
  helloAnroid() {

  }
}

class AndroidAppAdapter implements IOSAPP {
  app: AndroidApp;
  constructor(app: AndroidApp) {
    this.app = app;
  }
  show(name: string) {
    this.app.helloAnroid();
    return true;
  }
}
let androidApp = new AndroidApp();
let androidAppAdapter = new AndroidAppAdapter(androidApp);

let ios = new IOS();
ios.run(androidAppAdapter);

// Decorator

interface Coffee {
  getPrice(): number;
}

class SimpleCoffee implements Coffee {
  getPrice() {
    return 10;
  }
}

class MilkCoffee implements Coffee {
  coffee: Coffee;
  constructor(coffee: Coffee) {
    this.coffee = coffee;
  }
  getPrice() {
    return this.coffee.getPrice() + 5;
  }
}

let coffee = new SimpleCoffee();
coffee.getPrice();

let milkCoffee = new MilkCoffee(coffee);
milkCoffee.getPrice();

第五题

矩形相交判断

如果下面任一条件成立,则两个矩形不相交

  1. 其中一个矩形在另一个矩形的上面
  2. 其中一个矩形在另一个矩形的左边
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment