李恒道
发表于 2024-12-20 16:37:47
https://leetcode.cn/problems/evaluate-reverse-polish-notation/submissions/588296105/?envType=study-plan-v2&envId=top-interview-150
偷懒利用eval秒了
```js
var evalRPN = function (tokens) {
const stack = []
for (let index = 0; index < tokens.length; index++) {
const item = tokens;
if (item == '*' || item == '/' || item == "+" || item == '-') {
const item1=stack.pop();
const item2=stack.pop()
stack.push(parseInt(eval(`${item2} ${item} ${item1}`)))
} else {
stack.push(parseInt(item))
}
}
return parseInt(stack.pop())
};
```
李恒道
发表于 2024-12-20 16:39:11
王一之 发表于 2024-12-20 16:31
有排名了吗?
快300题了还是没排名
不过已经快兴致缺缺了...
越往后做提升越少,高分题做不动,低分题起手秒
中等的纯属猜解法
李恒道
发表于 2024-12-20 16:49:21
https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/submissions/588299340/?envType=study-plan-v2&envId=top-interview-150
一次过
```js
var buildTree = function (inorder, postorder) {
const generateTree = (il, ir, pl, pr) => {
if (il == ir) {
return {
val: inorder,
left: null,
right: null,
}
}
const val = postorder;
const node = {
val,
left: null,
right: null,
}
let iPos = -1;
for (let index = il; index <= ir; index++) {
if (inorder === val) {
iPos = index;
}
}
const leftLen = iPos - il;
const rightLen = ir - iPos;
if (leftLen !== 0) {
node.left = generateTree(il, iPos - 1, pl, pl + leftLen - 1)
}
if (rightLen !== 0) {
node.right = generateTree(iPos + 1, ir, pl + leftLen, pr - 1)
}
return node
}
return generateTree(0, postorder.length - 1, 0, postorder.length - 1)
};
```
李恒道
发表于 2024-12-20 17:11:55
https://leetcode.cn/problems/n-queens-ii/submissions/588300918/?envType=study-plan-v2&envId=top-interview-150
一次过
懒得位运算了
```js
var totalNQueens = function (n) {
const board = []
for (let index = 0; index < n; index++) {
board.push(new Array(n).fill('.'))
}
const checkValid = (x, y) => {
//上方
for (let index = 0; index < x; index++) {
const pos = board;
if (pos === 'Q') {
return false
}
}
//右斜方
for (let index = x - 1; index >= 0; index--) {
const posY = y + (x - index)
if (posY >= board.length) {
break;
}
const pos = board;
if (pos === 'Q') {
return false
}
}
//左斜方
for (let index = x - 1; index >= 0; index--) {
const posY = y - (x - index)
if (posY < 0) {
break;
}
const pos = board;
if (pos === 'Q') {
return false
}
}
return true;
}
let result=0
const dfs = (index) => {
if(index==board.length){
result++
return
}
const layout = board//第n行
for (let y = 0; y < layout.length; y++) {//第y列
if (checkValid(index, y)) {
board='Q'
dfs(index+1)
board='.'
}
}
}
dfs(0)
return result
};
```
李恒道
发表于 2024-12-20 18:24:37
https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/submissions/588319466/?envType=study-plan-v2&envId=top-interview-150
简单题
秒了
```js
var zigzagLevelOrder = function (root) {
if (root == null) {
return []
}
const ans = []
let stack = ;
let first = true
while (stack.length !== 0) {
let newStack = [];
let result = []
while (stack.length !== 0) {
const item = stack.shift();
first === true ? result.push(item.val) : result.unshift(item.val);
if (item.left !== null) {
newStack.push(item.left)
}
if (item.right !== null) {
newStack.push(item.right)
}
}
stack = newStack
first = !first
ans.push(result)
}
return ans
};
```
李恒道
发表于 2024-12-21 17:22:59
https://leetcode.cn/problems/course-schedule-ii/submissions/588464982/?envType=study-plan-v2&envId=top-interview-150
标准bfs,一次过
```js
var findOrder = function (numCourses, prerequisites) {
const outEdges = new Array(numCourses).fill(0).map(() => []);
const inEdges = new Array(numCourses).fill(0);
for (let index = 0; index < prerequisites.length; index++) {
const prerequisite = prerequisites;
outEdges].push(prerequisite)
inEdges]++;
}
const stack = [];
const ans = []
for (let index = 0; index < inEdges.length; index++) {
const item = inEdges;
if (item == 0) {
stack.push(index);
}
}
while (stack.length !== 0) {
const item = stack.pop();
ans.push(item);
const childrens = outEdges;
for (let index = 0; index < childrens.length; index++) {
const children = childrens;
inEdges--;
if(inEdges==0){
stack.push(children);
}
}
}
if(ans.length!==numCourses){
return []
}
return ans
};
```
李恒道
发表于 2024-12-21 17:23:20
https://leetcode.cn/problems/sort-the-students-by-their-kth-score/
今天的每日一题没前几天的难
```js
var sortTheStudents = function(score, k) {
returnscore.sort((a,b)=>b-a)
};
```
李恒道
发表于 2024-12-21 18:07:36
https://leetcode.cn/problems/construct-quad-tree/submissions/588471321/?envType=study-plan-v2&envId=top-interview-150
位置算错了扣个半个小时
我日
```js
var construct = function (grid) {
const gridTotal = new Array(grid.length).fill(0).map(() => new Array(grid.length).fill(0))
for (let index = 0; index < grid.length; index++) {
let total = 0
for (let indey = 0; indey < grid.length; indey++) {
const num = grid;
total += num
gridTotal = (gridTotal ?? 0) + total
}
}
const getArea = (x, y, len) => {
const all = gridTotal;
const sub1 = y - len < 0 ? 0 : gridTotal;
const sub2 = x - len < 0 ? 0 : gridTotal;
const add = x - len < 0 || y - len < 0 ? 0 : gridTotal;
return all - sub1 - sub2 + add
}
const isAllOneOrZero = (x, y, len) => {
const area = getArea(x, y, len);
if (area == 0 || area == (len * len)) {
return { isLeaf: true, val: area == 0 ? false : true }
}
return { isLeaf: false, val: false }
}
const generateTree = (x, y, len) => {
const node = isAllOneOrZero(x, y, len);
if (node.isLeaf) {
node.topLeft = node.topRight = node.bottomLeft = node.bottomRight = null;
} else {
const halfLen = parseInt(len / 2)
node.topLeft = generateTree(x - (halfLen), y - halfLen, halfLen)
node.topRight = generateTree(x- (halfLen),y, halfLen)
node.bottomLeft = generateTree(x, y - halfLen, halfLen)
node.bottomRight = generateTree(x, y, halfLen)
}
return node
}
return generateTree(grid.length - 1, grid.length - 1, grid.length)
};
```
李恒道
发表于 2024-12-22 02:49:07
https://leetcode.cn/problems/richest-customer-wealth/submissions/588536600/?envType=study-plan-v2&envId=programming-skills
水题秒一个玩
```js
func maximumWealth(accounts [][]int) int {
var ans int
for i := 0; i < len(accounts); i++ {
var total int = 0
for index := 0; index < len(accounts); index++ {
total += accounts
}
ans = int(math.Max(float64(ans), float64(total)))
}
return ans
}
```
李恒道
发表于 2024-12-22 02:52:09
https://leetcode.cn/problems/matrix-diagonal-sum/submissions/588536659/?envType=study-plan-v2&envId=programming-skills
```js
func diagonalSum(mat [][]int) int {
var ans int
for i := 0; i < len(mat); i++ {
ans += mat
if i!=len(mat)-i-1 {
ans += mat
}
}
return ans
}
```