高频算法面试题

合并两个有序数组

js 复制代码
const merge = (nums1, nums2) => {
  let p1 = 0;
  let p2 = 0;
  const result = [];
  let cur;
  while (p1 < nums1.length || p2 < nums2.length) {
    if (p1 === nums1.length) {
      cur = nums2[p2++];
    } else if (p2 === nums2.length) {
      cur = nums1[p1++];
    } else if (nums1[p1] < nums2[p2]) {
      cur = nums1[p1++];
    } else {
      cur = nums2[p2++];
    }
    result[p1 + p2 - 1] = cur;
  }
  return result;
}

console.log(merge([1, 3, 5], [2, 4, 6, 8]);
// Output: [1, 2, 3, 4, 5, 6, 8 ]

计算数组的所有子集

js 复制代码
function subsets(nums) {
  let result = []
  result.push([])
  for (const num of nums) {
    let newSubsets = []
    for (const subset of result) {
      let newSubset = subset.slice()
      newSubset.push(num)
      newSubsets.push(newSubset)
    }
    result.push(...newSubsets)
  }
  return result
}

console.log(subsets([1,2,3]))
// Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

实现数组全排列算法

js 复制代码
function permute(nums) {
  let result = [];

  const backtrack = (arr, set) => {
    if (set.size === nums.length) {
      result.push([...set]);
      return;
    }
    for (let i = 0; i < arr.length; i++) {
      if (set.has(arr[i])) continue;
      set.add(arr[i]);
      backtrack(arr, set);
      set.delete(arr[i]);
    }
  }

  backtrack(nums, new Set());

  return result;
}

// 示例:
console.log(permute([1, 2, 3]));
// Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

SKU 计算(类似于多个数组的排列组合)

js 复制代码
function permuteArrays(arrays) {
  if (arrays.length === 0) return [[]];

  const currentArray = arrays[0];
  const remainingArrays = arrays.slice(1);
  const permutations = [];

  const permsWithoutCurrent = permuteArrays(remainingArrays);

  for (let i = 0; i < currentArray.length; i++) {
    for (let j = 0; j < permsWithoutCurrent.length; j++) {
      const permutation = [currentArray[i], ...permsWithoutCurrent[j]];
      permutations.push(permutation);
    }
  }

  return permutations;
}

// example usage
const arr1 = [1, 2];
const arr2 = ['a', 'b'];
const arr3 = ['x', 'y'];

const allPermutations = permuteArrays([arr1, arr2, arr3]);

console.log(allPermutations); // prints [[1, "a", "x"], [1, "a", "y"], [1, "b", "x"], [1, "b", "y"], [2, "a", "x"], [2, "a", "y"], [2, "b", "x"], [2, "b", "y"]

阿拉伯数字转换为中文

js 复制代码
function arabicToChinese(num) {
  var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  var chnUnitChar = ["", "十", "百", "千", "万", "亿", "万亿", "亿亿"];
  var strIns = "",
    chnStr = "";
  var unitPos = 0;
  var zero = true;
  while (num > 0) {
    var v = num % 10;
    if (v === 0) {
      if (!zero) {
        zero = true;
        chnStr = chnNumChar[v] + chnStr;
      }
    } else {
      zero = false;
      strIns = chnNumChar[v];
      strIns += chnUnitChar[unitPos];
      chnStr = strIns + chnStr;
    }
    unitPos++;
    num = Math.floor(num / 10);
  }
  return chnStr;
}

console.log(arabicToChinese(1001));
// Output: 一千零一

把属性名转换为驼峰

js 复制代码
let obj = {a_b: {a_b: "a_b",}};

function toCamelCase(obj) {
  if (Array.isArray(obj)) {
    return obj.map((v) => toCamelCase(v));
  } else if (obj !== null && typeof obj === "object") {
    return Object.keys(obj).reduce(
      (result, key) => ({
        ...result,
        [key.replace(/(_\w)/g, (matches) => matches[1].toUpperCase())]:
          toCamelCase(obj[key]),
      }),
      {}
    );
  }
  return obj;
}

console.log(toCamelCase(obj))
// Output: { aB: { aB: 'a_b' } }

千位分隔数

js 复制代码
var thousandSeparator = function(n) {
    let count = 0;
    let ans = "";
    do {
        let cur = n % 10;
        n = Math.floor(n / 10);
        ans += cur.toString();
        ++count;
        if (count % 3 == 0 && n) {
            ans += ',';
        }
    } while (n);
    return ans.split('').reverse().join('');
};

console.log(thousandSeparator(1234))
// Output: 1,234

无重复字符的最长子串

js 复制代码
var lengthOfLongestSubstring = function(s) {
  const set = new Set()
  let max = 0
  for (let i = 0; i < s.length; i++) {
    set.clear()
    for (let j = i; j < s.length; j++) {
      const item = s[j]
      if (set.has(item)) {
        break
      } else {
        set.add(item)
      }
    }
    max = Math.max(max, set.size)
  }
  return max
};

最长连续递增子序列

js 复制代码
var findLengthOfLCIS = function (nums) {
  let ans = 0;
  const n = nums.length;
  let start = 0;
  for (let i = 0; i < n; i++) {
    if (i > 0 && nums[i] <= nums[i - 1]) {
      start = i;
    }
    ans = Math.max(ans, i - start + 1);
  }
  return ans;
};
console.log(findLengthOfLCIS([1,3,5,4,7]))
// Output: 3

并发数量控制

js 复制代码
class Schedule {
  constructor(maxNum) {
    this.list = [];
    this.maxNum = maxNum
    this.workingNum = 0
  }
  add(promiseCreator) {
    this.list.push(promiseCreator)
  }
  start() {
    for (let index = 0; index < this.maxNum; index++) {
      this.doNext()
    }
  }
  doNext() {
    if (this.list.length && this.workingNum < this.maxNum) {
      this.workingNum++;
      const promise = this.list.shift();
      promise().then(() => {
        this.workingNum--;
        this.doNext();
      })
    }
  }
}

const timeout = time => new Promise((resolve) => {
  setTimeout(resolve, time)
})

const schedule = new Schedule(2);

const addTask = (time, order) => {
  schedule.add(() => timeout(time).then(() => {
    console.log(order);
  }))
}

addTask(1000, 1)
addTask(500, 2)
addTask(300, 3)
addTask(400, 4)

schedule.start()

字符串相加

js 复制代码
function addStrings (num1, num2) {
  let i = num1.length - 1
  let j = num2.length - 1
  let add = 0
  const ans = []
  while (i >= 0 || j >= 0 || add !== 0) {
    const x = i >= 0 ? +num1[i] : 0
    const y = j >= 0 ? +num2[j] : 0
    const result = x + y + add
    ans.push(result % 10)
    add = Math.floor(result / 10)
    i -= 1
    j -= 1
  }
  return ans.reverse().join('')
}
console.log(addStrings('11', '123'))
// Output: '134'

x 的平方根

js 复制代码
var mySqrt = function (x) {
  let l = 0;
  let r = x;
  let ans = -1;
  while (l <= r) {
    let mid = (l + r) >> 1;
    if (mid * mid <= x) {
      ans = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return ans;
};
console.log(mySqrt(4))
// Output: 2

二叉树翻转

js 复制代码
function reverse(node) {
  if (node != null) {
    let temp = node.left;
    node.left = node.right;
    node.right = temp;
    reverse(node.left);
    reverse(node.right);
  }
}

合并二叉树

js 复制代码
function mergeTrees(t1, t2) {
  if (t1 == null)
    return t2;
  if (t2 == null)
    return t1;
  t1.value += t2.value;
  let left = mergeTrees(t1.left, t2.left);
  if (left) {
    t1.left = left
  }
  let right = mergeTrees(t1.right, t2.right);
  if (right) {
    t1.right = right
  }
  return t1;
}
console.log(mergeTrees(rootA, rootB))

判断对称二叉树

js 复制代码
function isSymmetric (root) {
  return isMirror(root, root)
}

function isMirror (t1, t2) {
  if (t1 == null && t2 == null) return true;
  if (t1 == null || t2 == null) return false;
  return (t1.value === t2.value) && isMirror(t1.right, t2.left) && isMirror(t1.left, t2.right)
}

console.log(isSymmetric(node1))

随机打乱算法

js 复制代码
function gen(w) {
  const arr = [];
  for (let i = 0; i < w * 10000; i++) {
    arr[i] = i + 1
  }
  shuffle(arr)
  return arr
}

// O(n) 的打乱算法
function shuffle(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    // [i, arr.length-1] 取一个整数
    const j = i + Math.floor(Math.random() * (arr.length - i));
    [arr[i], arr[j]] = [arr[j], arr[i]]
  }
  return arr
}

console.log(Math.floor(Math.random() * 5))

// 错误写法
function shuffle(arr) {
  return arr.sort(() => Math.random() - 0.5)
}
相关推荐
你挚爱的强哥1 小时前
✅✅✅【Vue.js】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本
javascript·vue.js·jquery
LNTON羚通1 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
y先森1 小时前
CSS3中的伸缩盒模型(弹性盒子、弹性布局)之伸缩容器、伸缩项目、主轴方向、主轴换行方式、复合属性flex-flow
前端·css·css3
前端Hardy1 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189111 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
IT女孩儿2 小时前
CSS查缺补漏(补充上一条)
前端·css
哭泣的眼泪4083 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
Microsoft Word3 小时前
c++基础语法
开发语言·c++·算法
吃杠碰小鸡3 小时前
commitlint校验git提交信息
前端
天才在此4 小时前
汽车加油行驶问题-动态规划算法(已在洛谷AC)
算法·动态规划