数据结构
二叉树
const root = {
val: 1,
left: {
var: 2,
left: {
val: 4
},
rigth: {
val: 5
}
},
right: {
val: 3,
left: {
val: 6
},
right: {
val: 7
}
}
}
二叉树非递归前序遍历
function preOrder(node) {
if(!node) return [];
let stack = [];
let res = [];
stack.push(node);
while(stack.length != 0) {
let temp = stack.pop();
res.push(temp.val);
if(temp.right) {
stack.push(temp.right);
}
if(temp.left) {
stack.push(temp.left);
}
}
return res;
}
二叉树非递归中序遍历
function inOrder(node) {
if(!node) return [];
let stack = [];
let res = [];
while(true) {
while(node) {
stack.push(node);
node = node.left;
}
if(stack.length === 0) break;
let temp = stack.pop();
res.push(temp.val);
node = temp.right;
}
return res;
}
二叉树非递归后序遍历
function afterOrder(node) {
if(!node) return [];
let stack = [];
let res = [];
stack.push(node);
while(stack.length != 0) {
let temp = stack.pop();
res.push(temp.val);
if(temp.left) {
stack.push(temp.left);
}
if(temp.right) {
stack.push(temp.right);
}
}
return res.reverse();
}
二叉树层级遍历
function levelOrder(node) {
if(!node) return [];
let res = [];
let queue = [node];
while(queue.length) {
let current = [];
let size = queue.length;
while(size --) {
let temp = queue.shift();
current.push(temp.val);
if(temp.left) queue.push(temp.left);
if(temp.right) queue.push(temp.right);
}
res.push(current);
}
return res;
}
二叉树锯齿形层次遍历
function zigzagLevelOrder(node) {
if(!node) return [];
let queue = [];
let res = [];
let level = 0;
queue.push(node);
while(queue.length) {
let size = queue.length;
let current = [];
while(size --) {
let temp = queue.shift();
current.push(temp.val);
if(temp.left) queue.push(temp.left);
if(temp.right) queue.push(temp.right);
}
if(level % 2 === 0) {
res.push(current);
} else {
res.push(current.reverse());
}
level ++;
}
return res;
}
算法
动态规划
零钱问题
币值分别为1、5、11,要凑够15元,最少多少个币
let coinChange = (coins, amount) => {
const dp = new Array(amount + 1).fill(Infinity)
dp[0] = 0
for (var i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1)
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
}
Easy
找出数组重复最多的元素
/**
* @param arr [2, 3, 4, 5, 5]
* @return: return highestFrequency item 5
*/
function highestFrequence(arr) {
let res;
let max = 0;
let obj = [];
for(let i = 0; i < arr.length; i++) {
if(!obj[arr[i]]) {
obj[arr[i]] = 1;
} else {
obj[arr[i]] += 1;
}
if(max < obj[arr[i]]) {
max = obj[arr[i]];
res = arr[i];
}
}
return res;
}
水仙花数
定义:一个 N 位非负整数,其各位数字的 N 次方和等于该数本身。
描述: 给出n,找到所有的n位十进制水仙花数。
#样例: 比如 n = 1, 所有水仙花数为:[0,1,2,3,4,5,6,7,8,9]。
而对于 n = 2, 则没有 2 位的水仙花数,返回 []。
function ifNarcissitic(num) {
let arr = num.toString().split('');
let res = 0;
let len = arr.length;
for(let i = 0; i < len; i++) {
res += Math.pow(Number(arr[i]), len);
}
if(num === res) return true;
return false;
}
function findNarcissitic(n) {
let res = [];
if(n < 0) {
return -1;
} else {
let arr = n === 1 ? 0 : Math.pow(10, (n - 1));
while(arr.toString().length === n) {
if(ifNarcissitic(arr)) {
res.push(arr);
}
arr += 1;
}
}
return res;
}
数组拍平
已知如下数组:
var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组
function flat(arr) {
let res = arr.toString().split(',').map((v)=>{return parseInt(v)}).sort((a, b)=> {return a - b});
return Array.from(new Set(res));
}
Medium
LeetCode 63. Unique Paths II
一个机器人位于一个m x n网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示。 说明:m和n的值均不超过 100。
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
let n = obstacleGrid.length;
if(!n) {
return 0;
}
let m = obstacleGrid[0].length;
if(obstacleGrid[0][0] === 1 || obstacleGrid[n - 1][m - 1] === 1) {
return 0;
}
let road = [];
for(let i = 0; i < n; i ++) {
road[i] = [];
for(let j = 0; j < m; j++) {
if(obstacleGrid[i][j] === 1) {
road[i][j] = 0;
} else if(i === 0) {
road[i][j] = road[i][j - 1] === 0 ? 0 : 1;
} else if( j === 0) {
road[i][j] = road[i - 1][j] === 0 ? 0 : 1;
} else {
road[i][j] = road[i - 1][j] + road[i][j - 1];
}
}
}
return road[n - 1][m - 1];
};
Hard
LeetCode 76.最小覆盖子串
给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字母的最小子串。
示例:
输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"
说明:
如果 S 中不存这样的子串,则返回空字符串 ""。 如果 S 中存在这样的子串,我们保证它是唯一的答案。