1.快速排序

取出数组第一个元素,以这个数为基准,比它小的放左边,比它大的放右边,然后对左右两边的数组分别递归,得到一个升序数组:

const quickSort = (arr) => {
  if (arr.length <= 1) return arr
  let firstItem = arr.shift()
  let leftArr = []
  let rightArr = []
  for (let item of arr) {
    if (item <= firstItem) {
      leftArr.push(item)
    } else {
      rightArr.push(item)
    }
  }
  return quickSort(leftArr).concat(firstItem, quickSort(rightArr))
}

console.log(quickSort([5, 17, 4, 22, 54, 55, 32]))

2.冒泡排序

const bubble = (arr) => {
  let tem
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        tem = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = tem
      }
    }
  }
  return arr
}

console.log(bubble([5, 17, 4, 22, 54, 55, 32]))

3.爬楼梯(db)

爬楼梯每次可以爬 1 层或者 2 层,那么爬 n 层有多少种方法:

const climb = (n) => {
  if (n === 1) return 1
  if (n === 2) return 2
  // 如果要爬到第 n 层,可以分为从第 n-1 层爬上去或者 n-2 层直接爬上去两种
  return climb(n - 1) + climb(n - 2)
}

console.log(climb(3), climb(4), climb(5))

4.跳跃游戏(贪心算法)

给定一个非负整数数组,你最初位于数组的第一个位置;数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个位置:

const jump = (arr) => {
  let length = 0
  for (let i = 0; i < arr.length; i++) {
    // 到了一个不可能到达的点
    if (i > length) return false
    length = Math.max(arr[i] + 1, length)
    if (length >= arr.length) return true
  }
  return true
}
console.log(jump([1, 3, 1, 1, 2])) // true
console.log(jump([1, 3, 1, 1, 1, 0, 2])) // false

5.去除相邻的重复字母

function testString(string) {
    let strArr = string.split("");
    let finalArr = [];
    let lastLetter;
    strArr.forEach((item) => {
    if (!finalArr.length) {
        // 如果当前 finalArr 为空则直接 push 一个元素
        finalArr.push(item);
    } else {
        // pop 出最后一位元素,如果和新的元素相等则不做处理,否则同时 push 这两个元素
        lastLetter = finalArr.pop();
        if (lastLetter !== item) {
        finalArr.push.apply(finalArr, [lastLetter, item]);
        }
    }
    });
    return finalArr.join("");
}
let string = "wgshtsbfvdccdnk";
console.log(testString(string)); // wgshtsbfvnk

6.手写 Promise

class myPromise {
  constructor(executor) {
    this.state = "pendding";
    this.reason = null;
    this.result = null;
    this.onFulfilled = null;
    this.onRejected = null;

    let resolve = (value) => {
      if (this.state === "pendding") {
        this.state = "fulfilled";
        this.result = value;
        this.onFulfilled(this.result);
      }
    };

    let reject = (reason) => {
      if (this.state === "pendding") {
        this.state = "rejected";
        this.reason = reason;
        this.onRejected(this.reason);
      }
    };

    try {
      executor(resolve, reject);
    } catch (error) {
      reject(error);
    }
  }

  then(onFulfilled, onReject) {
    if (this.state === "pendding") {
      this.onFulfilled = onFulfilled;
    } else if (this.state === "fulfilled") {
      onFulfilled(this.result);
    } else if (this.state === "rejected") {
      onReject(this.reason);
    }
  }
}

逃跑计划
8 声望1 粉丝

一个前端


« 上一篇
JavaScript 相关