李恒道
发表于 2024-8-25 00:43:42
https://leetcode.cn/problems/spiral-matrix/submissions/558280257/?envType=study-plan-v2&envId=top-100-liked
一眼分界,但是边界没有控制太好...
然而执行击败97%...
其他人到底怎么写的啊,喂
![图片.png](data/attachment/forum/202408/25/004334ujtt2pywtp2zpjfh.png)
```js
var spiralOrder = function (matrix) {
const result = []
const tiems = Math.ceil(Math.min(matrix.length, matrix.length) / 2)
for (let layout = 0; layout < tiems; layout++) {
const leftUpPos =
const rightDownPos = .length - layout - 1, matrix.length - layout - 1]
if (leftUpPos == rightDownPos && leftUpPos == rightDownPos) {
result.push(matrix]])
continue;
}
if (leftUpPos == rightDownPos) {
for (let posX = layout; posX <= rightDownPos; posX++) {
const num = matrix;
result.push(num)
}
continue;
}
if (leftUpPos == rightDownPos) {
for (let posY = leftUpPos; posY <= rightDownPos; posY++) {
const num = matrix];
result.push(num)
}
continue;
}
for (let posX = layout; posX < rightDownPos; posX++) {
const num = matrix;
result.push(num)
}
for (let posY = leftUpPos; posY < rightDownPos; posY++) {
const num = matrix];
result.push(num)
}
for (let posX = rightDownPos; posX > leftUpPos; posX--) {
const num = matrix];
result.push(num)
}
for (let posY = rightDownPos; posY > layout; posY--) {
const num = matrix;
result.push(num)
}
}
return result
};
```
李恒道
发表于 2024-8-25 01:38:07
https://leetcode.cn/problems/rotate-image/submissions/558285211/?envType=study-plan-v2&envId=top-100-liked
边界问题+轮换
简单
```js
var rotate = function (matrix) {
const result = []
const tiems = Math.ceil(Math.min(matrix.length, matrix.length) / 2)
for (let layout = 0; layout < tiems; layout++) {
const leftUpPos =
const rightDownPos = .length - layout - 1, matrix.length - layout - 1]
if (leftUpPos == rightDownPos && leftUpPos == rightDownPos) {
continue;
}
const distance=rightDownPos-leftUpPos
for (let posX = 0; posX < distance; posX++) {
const num1 = matrix + posX];
const num2 = matrix + posX]];
const num3 = matrix] - posX];
const num4 = matrix - posX];
matrix + posX]] = num1
matrix] - posX] = num2
matrix - posX] = num3
matrix + posX] = num4
}
}
return matrix
};
```
李恒道
发表于 2024-8-25 03:56:24
https://leetcode.cn/problems/linked-list-cycle/submissions/558290040/?envType=study-plan-v2&envId=top-100-liked
利用单步双步实现判断环形
```js
var hasCycle = function (head) {
if (head?.next == null || head?.next?.next == null) {
return false
}
let fast = head.next?.next
let slow = head.next
while (fast != slow && fast != null) {
fast = fast.next?.next
slow = slow.next
}
if (fast == slow) {
return true
}
return false
};
```
李恒道
发表于 2024-8-26 04:30:02
https://leetcode.cn/problems/search-a-2d-matrix-ii/submissions/558533072/?envType=study-plan-v2&envId=top-100-liked
以为是简单题
起手一个递归超时了...
然后上了一堆优化还超时
结果看到答案1可以直接暴力我人都傻了
```js
var searchMatrix = function (matrix, target) {
let x = 0, y = matrix.length - 1
while (x < matrix.length && y >= 0) {
const num = matrix
if (num == target) {
return true
} else if (num > target) {
y--
} else {
x++
}
}
return false
};
```
李恒道
发表于 2024-8-26 05:19:32
https://leetcode.cn/problems/palindrome-linked-list/submissions/558533622/?envType=study-plan-v2&envId=top-100-liked
简单回文,另外搞了一个简单的链表数字生成的,不然调试太差了
```js
const createNodeList = (arr) => {
let head = {
val: arr,
next: null
}
let ret = head
for (let index = 1; index < arr.length; index++) {
const num = arr;
head.next = {
val: num,
next: null
}
head = head.next
}
return ret
}
```
```js
var reverse = (head) => {
let ret = null
while (head != null) {
let node = head
head = head.next
node.next = ret
ret = node
}
return ret
}
/**
* @param {ListNode} head
* @Return {boolean}
*/
var isPalindrome = function (head) {
if (head.next == null) {
return true
}
let slow = head
let fast = head.next
if (slow != null && fast == null) {
return slow.val === head.val
}
while (fast?.next?.next != null) {
fast = fast?.next?.next
slow = slow.next
}
let step2 = slow.next
step2 = reverse(step2)
slow.next = null;
while (head?.val == step2?.val && head?.val != null) {
head = head.next
step2 = step2.next
}
if (head == null) {
return true
}
return false
};
```
王一之
发表于 2024-8-26 09:47:30
厉害了 这么久了
李恒道
发表于 2024-8-26 16:55:38
王一之 发表于 2024-8-26 09:47
厉害了 这么久了
才打了40题
mlgb到现在排名都没有....
王一之
发表于 2024-8-26 16:56:46
李恒道 发表于 2024-8-26 16:55
才打了40题
mlgb到现在排名都没有....
我记得上千道的,进入前1w名有难度的
王一之
发表于 2024-8-26 16:57:19
李恒道 发表于 2024-8-26 16:55
才打了40题
mlgb到现在排名都没有....
我提议新开一个catcode平台,重新进行排名
李恒道
发表于 2024-8-26 17:30:39
https://leetcode.cn/problems/linked-list-cycle-ii/submissions/558710983/?envType=study-plan-v2&envId=top-100-liked
定理题,很早之前死记硬背的
```js
var detectCycle = function (head) {
let slow = head?.next
let fast = head?.next?.next
if (fast == null) {
return null
}
while (fast !== slow) {
fast = fast?.next?.next
slow = slow?.next
}
if(fast===null){
return null
}
let line = head
while (line != slow) {
slow = slow?.next
line = line?.next
}
return line
};
```
页:
1
2
3
4
[5]
6
7
8
9
10
11
12
13
14