2022 最新字节跳动前端面试算法题 All In One
2022 最新字节跳动前端面试算法题 All In One
算法题-真题汇总
easy
简单
- 最长公共前缀 ?
https://leetcode.com/problems/longest-common-prefix/
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
let result = '';
let min = strs[0];
// 找到最小 str
for (const item of strs) {
if(item.length < min.length) {
min = item;
}
}
for (const char of Array.from(min)) {
// 判断每一个 str 的开头是否都包含该 chars
if(strs.every(str => str.startsWith(result + char))) {
result += char;
} else {
break;
}
}
return result;
};
function longestCommonPrefix(strs: string[]): string {
let sub = '';
let minStr = strs.sort((a, b) => a.length > b.length ? 1 : -1)[0];
for(let c of minStr) {
if(strs.every(str => str.startsWith(sub + c))) {
sub += c;
} else {
break;
}
}
return sub;
};
- 爬楼梯 ?
https://leetcode.com/problems/climbing-stairs/
- 平衡二叉树 ?
https://leetcode.com/problems/balanced-binary-tree/
- 只出现一次的数字 ?
https://leetcode.com/problems/single-number/
- 反转链表 ?
https://leetcode.com/problems/reverse-linked-list/
- 回文链表 ?
https://leetcode.com/problems/palindrome-linked-list/
- 数字转换为十六进制数 ?
https://leetcode.cn/problems/convert-a-number-to-hexadecimal/
- 斐波那契数 ?
https://leetcode.com/problems/fibonacci-number/
- 数组的度
https://leetcode.com/problems/degree-of-an-array/
- 二分查找
https://leetcode.com/problems/binary-search/
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
function search(nums: number[], target: number): number {
let left = 0;
// -1 防止越界 bug
let right = nums.length - 1;
while(left <= right) {
// let mid = Math.floor((left + right) / 2);
const mid = left + Math.floor((right - left) / 2);
if(nums[mid] === target) {
return mid;
} else if(nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
};
/*
[-1,0,3,5,9,12]
9
[-1,0,3,5,9,12]
2
[-1,0,3,5,9,12]
13
*/
- 寻找数组的中心下标 ?
https://leetcode.com/problems/find-pivot-index/
- 分割平衡字符串 ?
https://leetcode.com/problems/split-a-string-in-balanced-strings/
- 千位分隔数 ?
https://leetcode.com/problems/thousand-separator/
/**
* @param {number} n
* @return {string}
*/
var thousandSeparator = function(n) {
if(n < 1000) {
return `${n}`;
}
const arr = [...`${n}`].reverse();
for(let i = 3; i < arr.length; i++) {
if(i > 0 && i % 3 === 0) {
arr[i] = arr[i] + `.`;
}
}
return arr.reverse().join('');
};
// thousandSeparator(123456789)
// '123.456.789'
medium
中等
- 两数相加 ?
https://leetcode.com/problems/add-two-numbers/
- 无重复字符的最长子串 ?
https://leetcode.com/problems/longest-substring-without-repeating-characters/
时间复杂度: O(n), 一次 for 循环
空间复杂度: O(1), 两个变量
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(strs) {
let maxStr = '';
let maxLen = 0;
for(let str of strs) {
let index = maxStr.indexOf(str);
if(index > -1) {
// 如果存在,截取head
maxStr = maxStr.substr(index + 1);
}
// 追加 str
maxStr += str;
// 计算 max (防止截取到最后,maxLen > maxStr.length)
maxLen = Math.max(maxLen, maxStr.length)
}
return maxLen;
};
function lengthOfLongestSubstring(str: string): number {
let max: number = 0;
let temp: string = ``;
for (let s of str) {
if(!temp.includes(s)) {
temp += s;
} else {
// 截取,滑动移动
temp = temp.slice(temp.indexOf(s) + 1);
temp += s;
}
if(temp.length > max) {
max = temp.length;
}
}
return max;
};
- 括号生成 ?
https://leetcode.com/problems/generate-parentheses/
- 字符串相乘 ?
https://leetcode.com/problems/multiply-strings/
- 二叉树的层序遍历 ?
https://leetcode.com/problems/binary-tree-level-order-traversal/
- 求根节点到叶节点数字之和 ?
https://leetcode.com/problems/sum-root-to-leaf-numbers/
- 二叉树的右视图
https://leetcode.cn/problems/binary-tree-right-side-view/
- 岛屿数量 ?
https://leetcode.com/problems/number-of-islands/
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
// 二维数组
// 图,四周连接边数量 ???
// 图,邻接搜索 ???
let island = 0;
const rows = grid.length;
const cols = grid[0].length;
// DFS 深度优先搜索 & 递归
function dfs(grid, row, col) {
// const rows = grid.length;
// const cols = grid[0].length;
// 访问过的节点,值设置成 0 ?
grid[row][col] = '0';
// 越界判断,边界外都是 0
// top
if(row - 1 >= 0 && grid[row - 1][col] === '1') {
dfs(grid, row - 1, col);
}
// bottom
if(row + 1 < rows && grid[row + 1][col] === '1') {
dfs(grid, row + 1, col);
}
// left
if(col - 1 >= 0 && grid[row ][col - 1] === '1') {
dfs(grid, row, col - 1);
}
// right
if(col + 1 < cols && grid[row][col + 1] === '1') {
dfs(grid, row, col + 1);
}
}
for(const [i, arr] of grid.entries()) {
for(let j = 0; j < cols; j++) {
// console.log(`grid[i][j]`, grid[i][j]);
if(grid[i][j] === '1') {
island += 1;
dfs(grid, i, j);
}
}
}
return island;
};
// test case
// [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
// 1
// [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
// 3
- 二叉树的最近公共祖先 ?
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
- 最深叶节点的最近公共祖先 ?
https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
- 好叶子节点对的数量
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/
hard
较难
??? tree 遍历
算法题题库
字节跳动 - LeetCode
https://leetcode.cn/explore/interview/card/bytedance/
https://codetop.cc/home
https://github.com/afatcoder/LeetcodeTop/blob/master/bytedance/frontend.md
https://www.nowcoder.com/exam/company?tagIds=665
https://xiaochen1024.com/courseware/60b4f11ab1aa91002eb53b18
LeetCode 力扣
https://leetcode.com/list/
https://leetcode.cn/list/problem
LintCode 领扣 / 炼码
https://www.lintcode.com/
算法复杂度-大O表示法
Big(O) Notation
https://www.bigocheatsheet.com/
https://algodaily.com/lessons/understanding-big-o-and-algorithmic-complexity
Big O Complexity Graph
算法刷题原则
刷题难度: (2/8 原则,20% hard,80% medium / easy)
刷题数量:300 道+
刷题优先级:高频题、真题、
刷题算法类型:动态规划(DP)、递归、回溯、贪心、广度优先(BFS)、深度优先(DFS) ...
刷题数据结构:二叉树、链表、数组、字符串、堆、栈、队列、图 ...
面经
字节跳动算法部分其实没那么难,把 leetcode 热题100的简单和中等刷完就可以了,不需要刷困难题。??
多总结解题思路,回溯、动规、二叉树、递归是重点。
refs
图解算法数据结构 All In One
入字节跳动
https://job.bytedance.com/
https://zh.wikipedia.org/zh-hans/字节跳动
?xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!