[LeetCode] 迷宫问题(The Maze)
By Long Luo
490. 迷宫 Medium
505. 迷宫 II Medium 499. 迷宫 III Hard
490. The Maze


BFS
1 | class Solution { |
By Long Luo
490. 迷宫 Medium
505. 迷宫 II Medium 499. 迷宫 III Hard
1 | class Solution { |
By Long Luo
1 | public static int numIslands_bfs(char[][] grid) { |
1 | class Solution { |
By Long Luo
252. 会议室 253. 会议室 II
2402. 会议室 III
1 | public static boolean canAttendMeetings_bf(int[][] intervals) { |
1 | public static boolean canAttendMeetings(int[][] intervals) { |
By Long Luo
This article is the solution 4 Approaches: Two Pointers, Sorting, Priority Queue and Binary Search of Problem 658. Find K Closest Elements.
Here shows 4 Approaches to slove this problem: Two Pointers, Sorting, Priority Queue and Binary Search with Two Pointers.
1 | // TP time: O(n) space: O(1) |
By Long Luo
This article is the solution 3 Approaches: Brute Force, Stack, Slow Fast Pointers with Animation of Problem 19. Remove Nth Node From End of List .
Here shows 3 Approaches to slove this problem: Brute Force, Stack, Slow Fast Pointers.
It’s easy to think of the way that we traverse the linked list first, from the head node to the end node, to get the length of the linked list \(L\).
Then we traverse the linked list from the head node again. When the \(L-n+1\) th node is traversed, it is the node we need to delete.
We can traverse \(L-n+1\) nodes starting from the dummy node. When traversing to the \(L-n+1\)th node, its next node is the node we need to delete, so we only need to modify the pointer once to complete the deletion operation.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26public static ListNode removeNthFromEnd(ListNode head, int n) {
if (head.next == null && n == 1) {
return null;
}
List<ListNode> nodeList = new ArrayList<>();
ListNode pNode = head;
int size = 0;
while (pNode != null) {
nodeList.add(pNode);
size++;
pNode = pNode.next;
}
if (n == size) {
return head.next;
} else if (n == 1) {
pNode = nodeList.get(size - 2);
pNode.next = null;
} else {
pNode = nodeList.get(size - n - 1);
pNode.next = pNode.next.next;
}
return head;
}