Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/all.json # src/main/java/leetcode/editor/cn/translation.json
This commit is contained in:
commit
cb14981bc8
42
src/main/java/leet/book/queueStack/CanVisitAllRooms.java
Normal file
42
src/main/java/leet/book/queueStack/CanVisitAllRooms.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 16:09
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class CanVisitAllRooms {
|
||||||
|
class Solution {
|
||||||
|
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
|
||||||
|
int num = rooms.size();
|
||||||
|
List<Integer> use = new ArrayList<>();
|
||||||
|
Queue<Integer> queue = new LinkedList<>();
|
||||||
|
queue.offer(0);
|
||||||
|
use.add(0);
|
||||||
|
if (num == use.size()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int open = queue.poll();
|
||||||
|
for (int room : rooms.get(open)) {
|
||||||
|
if (use.contains(room)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
queue.offer(room);
|
||||||
|
use.add(room);
|
||||||
|
if (num == use.size()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
53
src/main/java/leet/book/queueStack/CloneGraph.java
Normal file
53
src/main/java/leet/book/queueStack/CloneGraph.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 13:43
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class CloneGraph {
|
||||||
|
class Node {
|
||||||
|
public int val;
|
||||||
|
public List<Node> neighbors;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
val = 0;
|
||||||
|
neighbors = new ArrayList<Node>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
neighbors = new ArrayList<Node>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val, ArrayList<Node> _neighbors) {
|
||||||
|
val = _val;
|
||||||
|
neighbors = _neighbors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private HashMap<Node, Node> use = new HashMap <> ();
|
||||||
|
public Node cloneGraph(Node node) {
|
||||||
|
if (node == null) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (use.containsKey(node)) {
|
||||||
|
return use.get(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node cloneNode = new Node(node.val, new ArrayList());
|
||||||
|
use.put(node, cloneNode);
|
||||||
|
|
||||||
|
for (Node neighbor: node.neighbors) {
|
||||||
|
cloneNode.neighbors.add(cloneGraph(neighbor));
|
||||||
|
}
|
||||||
|
return cloneNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
40
src/main/java/leet/book/queueStack/FloodFill.java
Normal file
40
src/main/java/leet/book/queueStack/FloodFill.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 14:14
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class FloodFill {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new FloodFill().new Solution();
|
||||||
|
// int[][] image = solution.floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2);
|
||||||
|
int[][] image = solution.floodFill(new int[][]{{0, 0, 0}, {0, 1, 1}}, 1, 1, 1);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
|
||||||
|
int oldColor = image[sr][sc];
|
||||||
|
if (oldColor != newColor) {
|
||||||
|
fill(image, sr, sc, newColor, oldColor);
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fill(int[][] image, int sr, int sc, int newColor, int oldColor) {
|
||||||
|
if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != oldColor) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
image[sr][sc] = newColor;
|
||||||
|
fill(image, sr - 1, sc, newColor, oldColor);
|
||||||
|
fill(image, sr + 1, sc, newColor, oldColor);
|
||||||
|
fill(image, sr, sc - 1, newColor, oldColor);
|
||||||
|
fill(image, sr, sc + 1, newColor, oldColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
src/main/java/leet/book/queueStack/MovingAverage.java
Normal file
45
src/main/java/leet/book/queueStack/MovingAverage.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 8:44
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class MovingAverage {
|
||||||
|
Queue<Integer> queue;
|
||||||
|
int size;
|
||||||
|
double sum = 0.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize your data structure here.
|
||||||
|
*/
|
||||||
|
public MovingAverage(int size) {
|
||||||
|
queue = new LinkedList<>();
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double next(int val) {
|
||||||
|
if (queue.size() >= size) {
|
||||||
|
sum += 1.0 * (val - queue.poll()) / size;
|
||||||
|
} else {
|
||||||
|
if (queue.isEmpty()) {
|
||||||
|
sum += val;
|
||||||
|
} else {
|
||||||
|
sum = (sum * queue.size() + val) / (queue.size() + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue.offer(val);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MovingAverage object will be instantiated and called as such:
|
||||||
|
* MovingAverage obj = new MovingAverage(size);
|
||||||
|
* double param_1 = obj.next(val);
|
||||||
|
*/
|
61
src/main/java/leet/book/queueStack/MyCircularQueue.java
Normal file
61
src/main/java/leet/book/queueStack/MyCircularQueue.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 8:44
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class MyCircularQueue {
|
||||||
|
|
||||||
|
private int[] data;
|
||||||
|
private int front, tail;
|
||||||
|
|
||||||
|
public MyCircularQueue(int k) {
|
||||||
|
data = new int[k + 1];
|
||||||
|
front = 0;
|
||||||
|
tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean enQueue(int value) {
|
||||||
|
if (isFull()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
data[tail] = value;
|
||||||
|
tail = (tail + 1) % data.length;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean deQueue() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
front = (front + 1) % data.length;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Front() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return data[front];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Rear() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return data[(tail - 1 + data.length) % data.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return front == tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFull() {
|
||||||
|
return (tail + 1) % data.length == front;
|
||||||
|
}
|
||||||
|
}
|
35
src/main/java/leet/book/queueStack/NumIslands.java
Normal file
35
src/main/java/leet/book/queueStack/NumIslands.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 10:53
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class NumIslands {
|
||||||
|
class Solution {
|
||||||
|
public int numIslands(char[][] grid) {
|
||||||
|
int count = 0;
|
||||||
|
for(int i = 0; i < grid.length; i++) {
|
||||||
|
for(int j = 0; j < grid[0].length; j++) {
|
||||||
|
if(grid[i][j] == '1'){
|
||||||
|
dfs(grid, i, j);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
private void dfs(char[][] grid, int x, int y){
|
||||||
|
if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == '0') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
grid[x][y] = '0';
|
||||||
|
dfs(grid, x + 1, y);
|
||||||
|
dfs(grid, x, y + 1);
|
||||||
|
dfs(grid, x - 1, y);
|
||||||
|
dfs(grid, x, y - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/main/java/leet/book/queueStack/UpdateMatrix.java
Normal file
52
src/main/java/leet/book/queueStack/UpdateMatrix.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 15:49
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class UpdateMatrix {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new UpdateMatrix().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[][] updateMatrix(int[][] mat) {
|
||||||
|
Queue<int[]> queue = new LinkedList<>();
|
||||||
|
int xLength = mat.length;
|
||||||
|
int yLength = mat[0].length;
|
||||||
|
boolean[][] use = new boolean[xLength][yLength];
|
||||||
|
for (int i = 0; i < xLength; i++) {
|
||||||
|
for (int j = 0; j < yLength; j++) {
|
||||||
|
if (mat[i][j] == 0) {
|
||||||
|
queue.offer(new int[]{i, j});
|
||||||
|
use[i][j] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int[][] ops = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int[] temp = queue.poll();
|
||||||
|
int x = temp[0];
|
||||||
|
int y = temp[1];
|
||||||
|
for (int[] op : ops) {
|
||||||
|
int tx = x + op[0];
|
||||||
|
int ty = y + op[1];
|
||||||
|
if (tx >= 0 && tx < xLength && ty >= 0 && ty < yLength && !use[tx][ty]) {
|
||||||
|
mat[tx][ty] = mat[x][y] + 1;
|
||||||
|
queue.offer(new int[]{tx,ty});
|
||||||
|
use[tx][ty]=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
src/main/java/leet/book/queueStack/WallsAndGates.java
Normal file
56
src/main/java/leet/book/queueStack/WallsAndGates.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package leet.book.queueStack;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author: 轩辕龙儿
|
||||||
|
* @date: 2021/7/2 10:46
|
||||||
|
* @Description: No Description
|
||||||
|
*/
|
||||||
|
public class WallsAndGates {
|
||||||
|
class Solution {
|
||||||
|
private static final int EMPTY = Integer.MAX_VALUE;
|
||||||
|
private static final int GATE = 0;
|
||||||
|
private final List<int[]> DIRECTIONS = Arrays.asList(
|
||||||
|
new int[]{1, 0},
|
||||||
|
new int[]{-1, 0},
|
||||||
|
new int[]{0, 1},
|
||||||
|
new int[]{0, -1}
|
||||||
|
);
|
||||||
|
|
||||||
|
public void wallsAndGates(int[][] rooms) {
|
||||||
|
int m = rooms.length;
|
||||||
|
if (m == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int n = rooms[0].length;
|
||||||
|
Queue<int[]> q = new LinkedList<>();
|
||||||
|
for (int row = 0; row < m; row++) {
|
||||||
|
for (int col = 0; col < n; col++) {
|
||||||
|
if (rooms[row][col] == GATE) {
|
||||||
|
q.add(new int[]{row, col});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (!q.isEmpty()) {
|
||||||
|
int[] point = q.poll();
|
||||||
|
int row = point[0];
|
||||||
|
int col = point[1];
|
||||||
|
for (int[] direction : DIRECTIONS) {
|
||||||
|
int r = row + direction[0];
|
||||||
|
int c = col + direction[1];
|
||||||
|
if (r < 0 || c < 0 || r >= m || c >= n || rooms[r][c] != EMPTY) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rooms[r][c] = rooms[row][col] + 1;
|
||||||
|
q.add(new int[]{r, c});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
89
src/main/java/leetcode/editor/cn/BalancedBinaryTree.java
Normal file
89
src/main/java/leetcode/editor/cn/BalancedBinaryTree.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
//给定一个二叉树,判断它是否是高度平衡的二叉树。
|
||||||
|
//
|
||||||
|
// 本题中,一棵高度平衡二叉树定义为:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [3,9,20,null,null,15,7]
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [1,2,2,3,3,null,null,4,4]
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = []
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 树中的节点数在范围 [0, 5000] 内
|
||||||
|
// -104 <= Node.val <= 104
|
||||||
|
//
|
||||||
|
// Related Topics 树 深度优先搜索 二叉树
|
||||||
|
// 👍 713 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
//110:平衡二叉树
|
||||||
|
public class BalancedBinaryTree{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new BalancedBinaryTree().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode() {}
|
||||||
|
* TreeNode(int val) { this.val = val; }
|
||||||
|
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
|
* this.val = val;
|
||||||
|
* this.left = left;
|
||||||
|
* this.right = right;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public boolean isBalanced(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
40
src/main/java/leetcode/editor/cn/BalancedBinaryTree.md
Normal file
40
src/main/java/leetcode/editor/cn/BalancedBinaryTree.md
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<p>给定一个二叉树,判断它是否是高度平衡的二叉树。</p>
|
||||||
|
|
||||||
|
<p>本题中,一棵高度平衡二叉树定义为:</p>
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<p>一个二叉树<em>每个节点 </em>的左右两个子树的高度差的绝对值不超过 1 。</p>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [1,2,2,3,3,null,null,4,4]
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = []
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>树中的节点数在范围 <code>[0, 5000]</code> 内</li>
|
||||||
|
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 713</li><li>👎 0</li></div>
|
72
src/main/java/leetcode/editor/cn/ChuanDiXinXi.java
Normal file
72
src/main/java/leetcode/editor/cn/ChuanDiXinXi.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
//小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 有 n 名玩家,所有玩家编号分别为 0 ~ n-1,其中小朋友 A 的编号为 0
|
||||||
|
// 每个玩家都有固定的若干个可传信息的其他玩家(也可能没有)。传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息)。
|
||||||
|
// 每轮信息必须需要传递给另一个人,且信息可重复经过同一个人
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 给定总玩家数 n,以及按 [玩家编号,对应可传递玩家编号] 关系组成的二维数组 relation。返回信息从小 A (编号 0 ) 经过 k 轮传递到编号
|
||||||
|
//为 n-1 的小伙伴处的方案数;若不能到达,返回 0。
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 输入:n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3
|
||||||
|
//
|
||||||
|
// 输出:3
|
||||||
|
//
|
||||||
|
// 解释:信息从小 A 编号 0 处开始,经 3 轮传递,到达编号 4。共有 3 种方案,分别是 0->2->0->4, 0->2->1->4, 0->2->
|
||||||
|
//3->4。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 输入:n = 3, relation = [[0,2],[2,1]], k = 2
|
||||||
|
//
|
||||||
|
// 输出:0
|
||||||
|
//
|
||||||
|
// 解释:信息不能从小 A 处经过 2 轮传递到编号 2
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 限制:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 2 <= n <= 10
|
||||||
|
// 1 <= k <= 5
|
||||||
|
// 1 <= relation.length <= 90, 且 relation[i].length == 2
|
||||||
|
// 0 <= relation[i][0],relation[i][1] < n 且 relation[i][0] != relation[i][1]
|
||||||
|
//
|
||||||
|
// Related Topics 深度优先搜索 广度优先搜索 图 动态规划
|
||||||
|
// 👍 89 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//LCP 07:传递信息
|
||||||
|
public class ChuanDiXinXi{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ChuanDiXinXi().new Solution();
|
||||||
|
System.out.println(solution.numWays(3,new int[][]{{0,2},{2,1}},2));
|
||||||
|
System.out.println(solution.numWays(5,new int[][]{{0,2},{2,1},{3,4},{2,3},{1,4},{2,0},{0,4}},3));
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int numWays(int n, int[][] relation, int k) {
|
||||||
|
int[][] dp =new int[k+1][n];
|
||||||
|
dp[0][0] =1;
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
for (int[] ints : relation) {
|
||||||
|
dp[i + 1][ints[1]] += dp[i][ints[0]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[k][n-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
39
src/main/java/leetcode/editor/cn/ChuanDiXinXi.md
Normal file
39
src/main/java/leetcode/editor/cn/ChuanDiXinXi.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<p>小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下:</p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>有 n 名玩家,所有玩家编号分别为 0 ~ n-1,其中小朋友 A 的编号为 0</li>
|
||||||
|
<li>每个玩家都有固定的若干个可传信息的其他玩家(也可能没有)。传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息)。</li>
|
||||||
|
<li>每轮信息必须需要传递给另一个人,且信息可重复经过同一个人</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p>给定总玩家数 <code>n</code>,以及按 <code>[玩家编号,对应可传递玩家编号]</code> 关系组成的二维数组 <code>relation</code>。返回信息从小 A (编号 0 ) 经过 <code>k</code> 轮传递到编号为 n-1 的小伙伴处的方案数;若不能到达,返回 0。</p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<p>输入:<code>n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3</code></p>
|
||||||
|
|
||||||
|
<p>输出:<code>3</code></p>
|
||||||
|
|
||||||
|
<p>解释:信息从小 A 编号 0 处开始,经 3 轮传递,到达编号 4。共有 3 种方案,分别是 0->2->0->4, 0->2->1->4, 0->2->3->4。</p>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
<p>输入:<code>n = 3, relation = [[0,2],[2,1]], k = 2</code></p>
|
||||||
|
|
||||||
|
<p>输出:<code>0</code></p>
|
||||||
|
|
||||||
|
<p>解释:信息不能从小 A 处经过 2 轮传递到编号 2</p>
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
<p><strong>限制:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 10</code></li>
|
||||||
|
<li><code>1 <= k <= 5</code></li>
|
||||||
|
<li><code>1 <= relation.length <= 90, 且 relation[i].length == 2</code></li>
|
||||||
|
<li><code>0 <= relation[i][0],relation[i][1] < n 且 relation[i][0] != relation[i][1]</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>图</li><li>动态规划</li></div></div>\n<div><li>👍 89</li><li>👎 0</li></div>
|
59
src/main/java/leetcode/editor/cn/ExcelSheetColumnTitle.java
Normal file
59
src/main/java/leetcode/editor/cn/ExcelSheetColumnTitle.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
//给定一个正整数,返回它在 Excel 表中相对应的列名称。
|
||||||
|
//
|
||||||
|
// 例如,
|
||||||
|
//
|
||||||
|
// 1 -> A
|
||||||
|
// 2 -> B
|
||||||
|
// 3 -> C
|
||||||
|
// ...
|
||||||
|
// 26 -> Z
|
||||||
|
// 27 -> AA
|
||||||
|
// 28 -> AB
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: 1
|
||||||
|
//输出: "A"
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: 28
|
||||||
|
//输出: "AB"
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
// 输入: 701
|
||||||
|
//输出: "ZY"
|
||||||
|
//
|
||||||
|
// Related Topics 数学 字符串
|
||||||
|
// 👍 370 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//168:Excel表列名称
|
||||||
|
public class ExcelSheetColumnTitle {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ExcelSheetColumnTitle().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public String convertToTitle(int columnNumber) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
while (columnNumber > 0) {
|
||||||
|
int num = (columnNumber - 1) % 26 + 1;
|
||||||
|
result.insert(0, (char)((num - 1) + 'A'));
|
||||||
|
columnNumber = (columnNumber - num) / 26;
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
32
src/main/java/leetcode/editor/cn/ExcelSheetColumnTitle.md
Normal file
32
src/main/java/leetcode/editor/cn/ExcelSheetColumnTitle.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<p>给定一个正整数,返回它在 Excel 表中相对应的列名称。</p>
|
||||||
|
|
||||||
|
<p>例如,</p>
|
||||||
|
|
||||||
|
<pre> 1 -> A
|
||||||
|
2 -> B
|
||||||
|
3 -> C
|
||||||
|
...
|
||||||
|
26 -> Z
|
||||||
|
27 -> AA
|
||||||
|
28 -> AB
|
||||||
|
...
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 1
|
||||||
|
<strong>输出:</strong> "A"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 28
|
||||||
|
<strong>输出:</strong> "AB"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 701
|
||||||
|
<strong>输出:</strong> "ZY"
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>数学</li><li>字符串</li></div></div>\n<div><li>👍 370</li><li>👎 0</li></div>
|
55
src/main/java/leetcode/editor/cn/MaxStack.md
Normal file
55
src/main/java/leetcode/editor/cn/MaxStack.md
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<p>设计一个最大栈数据结构,既支持栈操作,又支持查找栈中最大元素。</p>
|
||||||
|
|
||||||
|
<p>实现 <code>MaxStack</code> 类:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>MaxStack()</code> 初始化栈对象</li>
|
||||||
|
<li><code>void push(int x)</code> 将元素 x 压入栈中。</li>
|
||||||
|
<li><code>int pop()</code> 移除栈顶元素并返回这个元素。</li>
|
||||||
|
<li><code>int top()</code> 返回栈顶元素,无需移除。</li>
|
||||||
|
<li><code>int peekMax()</code> 检索并返回栈中最大元素,无需移除。</li>
|
||||||
|
<li><code>int popMax()</code> 检索并返回栈中最大元素,并将其移除。如果有多个最大元素,只要移除 <strong>最靠近栈顶</strong> 的那个。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入</strong>
|
||||||
|
["MaxStack", "push", "push", "push", "top", "popMax", "top", "peekMax", "pop", "top"]
|
||||||
|
[[], [5], [1], [5], [], [], [], [], [], []]
|
||||||
|
<strong>输出</strong>
|
||||||
|
[null, null, null, null, 5, 5, 1, 5, 1, 5]
|
||||||
|
|
||||||
|
<strong>解释</strong>
|
||||||
|
MaxStack stk = new MaxStack();
|
||||||
|
stk.push(5); // [<strong>5</strong>] - 5 既是栈顶元素,也是最大元素
|
||||||
|
stk.push(1); // [<strong>5</strong>, <strong>1</strong>] - 栈顶元素是 1,最大元素是 5
|
||||||
|
stk.push(5); // [5, 1, <strong>5</strong>] - 5 既是栈顶元素,也是最大元素
|
||||||
|
stk.top(); // 返回 5,[5, 1, <strong>5</strong>] - 栈没有改变
|
||||||
|
stk.popMax(); // 返回 5,[<strong>5</strong>, <strong>1</strong>] - 栈发生改变,栈顶元素不再是最大元素
|
||||||
|
stk.top(); // 返回 1,[<strong>5</strong>, <strong>1</strong>] - 栈没有改变
|
||||||
|
stk.peekMax(); // 返回 5,[<strong>5</strong>, <strong>1</strong>] - 栈没有改变
|
||||||
|
stk.pop(); // 返回 1,[<strong>5</strong>] - 此操作后,5 既是栈顶元素,也是最大元素
|
||||||
|
stk.top(); // 返回 5,[<strong>5</strong>] - 栈没有改变
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>-10<sup>7</sup> <= x <= 10<sup>7</sup></code></li>
|
||||||
|
<li>最多调用 <code>10<sup>4</sup></code> 次 <code>push</code>、<code>pop</code>、<code>top</code>、<code>peekMax</code> 和 <code>popMax</code></li>
|
||||||
|
<li>调用 <code>pop</code>、<code>top</code>、<code>peekMax</code> 或 <code>popMax</code> 时,栈中 <strong>至少存在一个元素</strong></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><b>进阶:</b> </p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>试着设计解决方案:调用 <code>top</code> 方法的时间复杂度为 <code>O(1)</code> ,调用其他方法的时间复杂度为 <code>O(logn)</code> 。 </li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>栈</li><li>设计</li><li>链表</li><li>双向链表</li><li>有序集合</li></div></div>\n<div><li>👍 70</li><li>👎 0</li></div>
|
131
src/main/java/leetcode/editor/cn/MaxStack1.java
Normal file
131
src/main/java/leetcode/editor/cn/MaxStack1.java
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
//设计一个最大栈数据结构,既支持栈操作,又支持查找栈中最大元素。
|
||||||
|
//
|
||||||
|
// 实现 MaxStack 类:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// MaxStack() 初始化栈对象
|
||||||
|
// void push(int x) 将元素 x 压入栈中。
|
||||||
|
// int pop() 移除栈顶元素并返回这个元素。
|
||||||
|
// int top() 返回栈顶元素,无需移除。
|
||||||
|
// int peekMax() 检索并返回栈中最大元素,无需移除。
|
||||||
|
// int popMax() 检索并返回栈中最大元素,并将其移除。如果有多个最大元素,只要移除 最靠近栈顶 的那个。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入
|
||||||
|
//["MaxStack", "push", "push", "push", "top", "popMax", "top", "peekMax", "pop",
|
||||||
|
// "top"]
|
||||||
|
//[[], [5], [1], [5], [], [], [], [], [], []]
|
||||||
|
//输出
|
||||||
|
//[null, null, null, null, 5, 5, 1, 5, 1, 5]
|
||||||
|
//
|
||||||
|
//解释
|
||||||
|
//MaxStack stk = new MaxStack();
|
||||||
|
//stk.push(5); // [5] - 5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.push(1); // [5, 1] - 栈顶元素是 1,最大元素是 5
|
||||||
|
//stk.push(5); // [5, 1, 5] - 5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.top(); // 返回 5,[5, 1, 5] - 栈没有改变
|
||||||
|
//stk.popMax(); // 返回 5,[5, 1] - 栈发生改变,栈顶元素不再是最大元素
|
||||||
|
//stk.top(); // 返回 1,[5, 1] - 栈没有改变
|
||||||
|
//stk.peekMax(); // 返回 5,[5, 1] - 栈没有改变
|
||||||
|
//stk.pop(); // 返回 1,[5] - 此操作后,5 既是栈顶元素,也是最大元素
|
||||||
|
//stk.top(); // 返回 5,[5] - 栈没有改变
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// -107 <= x <= 107
|
||||||
|
// 最多调用 104 次 push、pop、top、peekMax 和 popMax
|
||||||
|
// 调用 pop、top、peekMax 或 popMax 时,栈中 至少存在一个元素
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 试着设计解决方案:调用 top 方法的时间复杂度为 O(1) ,调用其他方法的时间复杂度为 O(logn) 。
|
||||||
|
//
|
||||||
|
// Related Topics 栈 设计 链表 双向链表 有序集合
|
||||||
|
// 👍 70 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
//716:最大栈
|
||||||
|
public class MaxStack1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new MaxStack().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class MaxStack {
|
||||||
|
Stack<Integer> stack;
|
||||||
|
Stack<Integer> max;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialize your data structure here.
|
||||||
|
*/
|
||||||
|
public MaxStack() {
|
||||||
|
stack = new Stack<>();
|
||||||
|
max = new Stack<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push(int x) {
|
||||||
|
stack.push(x);
|
||||||
|
if(max.isEmpty()||x>max.peek()){
|
||||||
|
max.push(x);
|
||||||
|
}else{
|
||||||
|
max.push(max.peek());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int pop() {
|
||||||
|
max.pop();
|
||||||
|
return stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int top() {
|
||||||
|
return stack.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int peekMax() {
|
||||||
|
return max.peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int popMax() {
|
||||||
|
int num = max.peek();
|
||||||
|
Stack<Integer> temp = new Stack<>();
|
||||||
|
while (stack.peek()!=num){
|
||||||
|
temp.push(pop());
|
||||||
|
}
|
||||||
|
pop();
|
||||||
|
while (!temp.isEmpty()){
|
||||||
|
push(temp.pop());
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your MaxStack object will be instantiated and called as such:
|
||||||
|
* MaxStack obj = new MaxStack();
|
||||||
|
* obj.push(x);
|
||||||
|
* int param_2 = obj.pop();
|
||||||
|
* int param_3 = obj.top();
|
||||||
|
* int param_4 = obj.peekMax();
|
||||||
|
* int param_5 = obj.popMax();
|
||||||
|
*/
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
94
src/main/java/leetcode/editor/cn/NumberOfIslands.java
Normal file
94
src/main/java/leetcode/editor/cn/NumberOfIslands.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
//给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。
|
||||||
|
//
|
||||||
|
// 岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
|
||||||
|
//
|
||||||
|
// 此外,你可以假设该网格的四条边均被水包围。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:grid = [
|
||||||
|
// ["1","1","1","1","0"],
|
||||||
|
// ["1","1","0","1","0"],
|
||||||
|
// ["1","1","0","0","0"],
|
||||||
|
// ["0","0","0","0","0"]
|
||||||
|
//]
|
||||||
|
//输出:1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:grid = [
|
||||||
|
// ["1","1","0","0","0"],
|
||||||
|
// ["1","1","0","0","0"],
|
||||||
|
// ["0","0","1","0","0"],
|
||||||
|
// ["0","0","0","1","1"]
|
||||||
|
//]
|
||||||
|
//输出:3
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// m == grid.length
|
||||||
|
// n == grid[i].length
|
||||||
|
// 1 <= m, n <= 300
|
||||||
|
// grid[i][j] 的值为 '0' 或 '1'
|
||||||
|
//
|
||||||
|
// Related Topics 深度优先搜索 广度优先搜索 并查集 数组 矩阵
|
||||||
|
// 👍 1212 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
//200:岛屿数量
|
||||||
|
public class NumberOfIslands {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new NumberOfIslands().new Solution();
|
||||||
|
System.out.println(solution.numIslands(new char[][]{
|
||||||
|
{'1', '1', '1', '1', '0'},
|
||||||
|
{'1', '1', '0', '1', '0'},
|
||||||
|
{'1', '1', '0', '0', '0'},
|
||||||
|
{'0', '0', '0', '0', '0'}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int numIslands(char[][] grid) {
|
||||||
|
int count = 0;
|
||||||
|
for(int i = 0; i < grid.length; i++) {
|
||||||
|
for(int j = 0; j < grid[0].length; j++) {
|
||||||
|
if(grid[i][j] == '1'){
|
||||||
|
dfs(grid, i, j);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
private void dfs(char[][] grid, int x, int y){
|
||||||
|
if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == '0') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
grid[x][y] = '0';
|
||||||
|
dfs(grid, x + 1, y);
|
||||||
|
dfs(grid, x, y + 1);
|
||||||
|
dfs(grid, x - 1, y);
|
||||||
|
dfs(grid, x, y - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
43
src/main/java/leetcode/editor/cn/NumberOfIslands.md
Normal file
43
src/main/java/leetcode/editor/cn/NumberOfIslands.md
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<p>给你一个由 <code>'1'</code>(陆地)和 <code>'0'</code>(水)组成的的二维网格,请你计算网格中岛屿的数量。</p>
|
||||||
|
|
||||||
|
<p>岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。</p>
|
||||||
|
|
||||||
|
<p>此外,你可以假设该网格的四条边均被水包围。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>grid = [
|
||||||
|
["1","1","1","1","0"],
|
||||||
|
["1","1","0","1","0"],
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["0","0","0","0","0"]
|
||||||
|
]
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>grid = [
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["1","1","0","0","0"],
|
||||||
|
["0","0","1","0","0"],
|
||||||
|
["0","0","0","1","1"]
|
||||||
|
]
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>m == grid.length</code></li>
|
||||||
|
<li><code>n == grid[i].length</code></li>
|
||||||
|
<li><code>1 <= m, n <= 300</code></li>
|
||||||
|
<li><code>grid[i][j]</code> 的值为 <code>'0'</code> 或 <code>'1'</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 1212</li><li>👎 0</li></div>
|
@ -0,0 +1,121 @@
|
|||||||
|
//序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方
|
||||||
|
//式重构得到原数据。
|
||||||
|
//
|
||||||
|
// 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串
|
||||||
|
//反序列化为原始的树结构。
|
||||||
|
//
|
||||||
|
// 提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的
|
||||||
|
//方法解决这个问题。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [1,2,3,null,null,4,5]
|
||||||
|
//输出:[1,2,3,null,null,4,5]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = []
|
||||||
|
//输出:[]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [1]
|
||||||
|
//输出:[1]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [1,2]
|
||||||
|
//输出:[1,2]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 树中结点数在范围 [0, 104] 内
|
||||||
|
// -1000 <= Node.val <= 1000
|
||||||
|
//
|
||||||
|
// Related Topics 树 深度优先搜索 广度优先搜索 设计 字符串 二叉树
|
||||||
|
// 👍 586 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
//297:二叉树的序列化与反序列化
|
||||||
|
public class SerializeAndDeserializeBinaryTree {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new SerializeAndDeserializeBinaryTree().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode(int x) { val = x; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public class Codec {
|
||||||
|
|
||||||
|
// Encodes a tree to a single string.
|
||||||
|
public String serialize(TreeNode root) {
|
||||||
|
String result = "";
|
||||||
|
if (root == null) {
|
||||||
|
return "null,";
|
||||||
|
}
|
||||||
|
result += root.val + ",";
|
||||||
|
result += serialize(root.left);
|
||||||
|
result += serialize(root.right);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decodes your encoded data to tree.
|
||||||
|
public TreeNode deserialize(String data) {
|
||||||
|
if ("null,".equals(data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String[] strs = data.split(",");
|
||||||
|
Queue<String> queue = new LinkedList<>();
|
||||||
|
for (String str : strs) {
|
||||||
|
queue.offer(str);
|
||||||
|
}
|
||||||
|
return des(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TreeNode des(Queue<String> queue) {
|
||||||
|
String value = queue.poll();
|
||||||
|
if ("null".equals(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode root = new TreeNode(Integer.parseInt(value));
|
||||||
|
root.left = des(queue);
|
||||||
|
root.right = des(queue);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Your Codec object will be instantiated and called as such:
|
||||||
|
// Codec ser = new Codec();
|
||||||
|
// Codec deser = new Codec();
|
||||||
|
// TreeNode ans = deser.deserialize(ser.serialize(root));
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
<p>序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。</p>
|
||||||
|
|
||||||
|
<p>请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
|
||||||
|
|
||||||
|
<p><strong>提示: </strong>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
|
||||||
|
<strong>输出:</strong>[1,2,3,null,null,4,5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = []
|
||||||
|
<strong>输出:</strong>[]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [1]
|
||||||
|
<strong>输出:</strong>[1]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [1,2]
|
||||||
|
<strong>输出:</strong>[1,2]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>树中结点数在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||||
|
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>设计</li><li>字符串</li><li>二叉树</li></div></div>\n<div><li>👍 586</li><li>👎 0</li></div>
|
75
src/main/java/leetcode/editor/cn/TwoVYnGI.java
Normal file
75
src/main/java/leetcode/editor/cn/TwoVYnGI.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
//小扣在秋日市集选择了一家早餐摊位,一维整型数组 `staple` 中记录了每种主食的价格,一维整型数组 `drinks` 中记录了每种饮料的价格。小扣的计划
|
||||||
|
//选择一份主食和一款饮料,且花费不超过 `x` 元。请返回小扣共有多少种购买方案。
|
||||||
|
//
|
||||||
|
//注意:答案需要以 `1e9 + 7 (1000000007)` 为底取模,如:计算初始结果为:`1000000008`,请返回 `1`
|
||||||
|
//
|
||||||
|
//**示例 1:**
|
||||||
|
//>输入:`staple = [10,20,5], drinks = [5,5,2], x = 15`
|
||||||
|
//>
|
||||||
|
//>输出:`6`
|
||||||
|
//>
|
||||||
|
//>解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
|
||||||
|
//>第 1 种方案:staple[0] + drinks[0] = 10 + 5 = 15;
|
||||||
|
//>第 2 种方案:staple[0] + drinks[1] = 10 + 5 = 15;
|
||||||
|
//>第 3 种方案:staple[0] + drinks[2] = 10 + 2 = 12;
|
||||||
|
//>第 4 种方案:staple[2] + drinks[0] = 5 + 5 = 10;
|
||||||
|
//>第 5 种方案:staple[2] + drinks[1] = 5 + 5 = 10;
|
||||||
|
//>第 6 种方案:staple[2] + drinks[2] = 5 + 2 = 7。
|
||||||
|
//
|
||||||
|
//**示例 2:**
|
||||||
|
//>输入:`staple = [2,1,1], drinks = [8,9,5,1], x = 9`
|
||||||
|
//>
|
||||||
|
//>输出:`8`
|
||||||
|
//>
|
||||||
|
//>解释:小扣有 8 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
|
||||||
|
//>第 1 种方案:staple[0] + drinks[2] = 2 + 5 = 7;
|
||||||
|
//>第 2 种方案:staple[0] + drinks[3] = 2 + 1 = 3;
|
||||||
|
//>第 3 种方案:staple[1] + drinks[0] = 1 + 8 = 9;
|
||||||
|
//>第 4 种方案:staple[1] + drinks[2] = 1 + 5 = 6;
|
||||||
|
//>第 5 种方案:staple[1] + drinks[3] = 1 + 1 = 2;
|
||||||
|
//>第 6 种方案:staple[2] + drinks[0] = 1 + 8 = 9;
|
||||||
|
//>第 7 种方案:staple[2] + drinks[2] = 1 + 5 = 6;
|
||||||
|
//>第 8 种方案:staple[2] + drinks[3] = 1 + 1 = 2;
|
||||||
|
//
|
||||||
|
//**提示:**
|
||||||
|
//+ `1 <= staple.length <= 10^5`
|
||||||
|
//+ `1 <= drinks.length <= 10^5`
|
||||||
|
//+ `1 <= staple[i],drinks[i] <= 10^5`
|
||||||
|
//+ `1 <= x <= 2*10^5` Related Topics 数组 双指针 二分查找 排序
|
||||||
|
// 👍 50 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//LCP 18:早餐组合
|
||||||
|
public class TwoVYnGI {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new TwoVYnGI().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int breakfastNumber(int[] staple, int[] drinks, int x) {
|
||||||
|
int[] array = new int[x + 1];
|
||||||
|
for (int num : staple) {
|
||||||
|
if (num < x) {
|
||||||
|
array[num]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 1; i < x + 1; i++) {
|
||||||
|
array[i] += array[i - 1];
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (int num : drinks) {
|
||||||
|
if (num < x) {
|
||||||
|
count += array[x - num];
|
||||||
|
count %= 1000000007;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
37
src/main/java/leetcode/editor/cn/TwoVYnGI.md
Normal file
37
src/main/java/leetcode/editor/cn/TwoVYnGI.md
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
小扣在秋日市集选择了一家早餐摊位,一维整型数组 `staple` 中记录了每种主食的价格,一维整型数组 `drinks` 中记录了每种饮料的价格。小扣的计划选择一份主食和一款饮料,且花费不超过 `x` 元。请返回小扣共有多少种购买方案。
|
||||||
|
|
||||||
|
注意:答案需要以 `1e9 + 7 (1000000007)` 为底取模,如:计算初始结果为:`1000000008`,请返回 `1`
|
||||||
|
|
||||||
|
**示例 1:**
|
||||||
|
>输入:`staple = [10,20,5], drinks = [5,5,2], x = 15`
|
||||||
|
>
|
||||||
|
>输出:`6`
|
||||||
|
>
|
||||||
|
>解释:小扣有 6 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
|
||||||
|
>第 1 种方案:staple[0] + drinks[0] = 10 + 5 = 15;
|
||||||
|
>第 2 种方案:staple[0] + drinks[1] = 10 + 5 = 15;
|
||||||
|
>第 3 种方案:staple[0] + drinks[2] = 10 + 2 = 12;
|
||||||
|
>第 4 种方案:staple[2] + drinks[0] = 5 + 5 = 10;
|
||||||
|
>第 5 种方案:staple[2] + drinks[1] = 5 + 5 = 10;
|
||||||
|
>第 6 种方案:staple[2] + drinks[2] = 5 + 2 = 7。
|
||||||
|
|
||||||
|
**示例 2:**
|
||||||
|
>输入:`staple = [2,1,1], drinks = [8,9,5,1], x = 9`
|
||||||
|
>
|
||||||
|
>输出:`8`
|
||||||
|
>
|
||||||
|
>解释:小扣有 8 种购买方案,所选主食与所选饮料在数组中对应的下标分别是:
|
||||||
|
>第 1 种方案:staple[0] + drinks[2] = 2 + 5 = 7;
|
||||||
|
>第 2 种方案:staple[0] + drinks[3] = 2 + 1 = 3;
|
||||||
|
>第 3 种方案:staple[1] + drinks[0] = 1 + 8 = 9;
|
||||||
|
>第 4 种方案:staple[1] + drinks[2] = 1 + 5 = 6;
|
||||||
|
>第 5 种方案:staple[1] + drinks[3] = 1 + 1 = 2;
|
||||||
|
>第 6 种方案:staple[2] + drinks[0] = 1 + 8 = 9;
|
||||||
|
>第 7 种方案:staple[2] + drinks[2] = 1 + 5 = 6;
|
||||||
|
>第 8 种方案:staple[2] + drinks[3] = 1 + 1 = 2;
|
||||||
|
|
||||||
|
**提示:**
|
||||||
|
+ `1 <= staple.length <= 10^5`
|
||||||
|
+ `1 <= drinks.length <= 10^5`
|
||||||
|
+ `1 <= staple[i],drinks[i] <= 10^5`
|
||||||
|
+ `1 <= x <= 2*10^5`<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>二分查找</li><li>排序</li></div></div>\n<div><li>👍 50</li><li>👎 0</li></div>
|
114
src/main/java/leetcode/editor/cn/WallsAndGates.java
Normal file
114
src/main/java/leetcode/editor/cn/WallsAndGates.java
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
//你被给定一个 m × n 的二维网格 rooms ,网格中有以下三种可能的初始化值:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// -1 表示墙或是障碍物
|
||||||
|
// 0 表示一扇门
|
||||||
|
// INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647
|
||||||
|
//的。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 你要给每个空房间位上填上该房间到 最近门的距离 ,如果无法到达门,则填 INF 即可。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1]
|
||||||
|
//,[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
|
||||||
|
//输出:[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:rooms = [[-1]]
|
||||||
|
//输出:[[-1]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:rooms = [[2147483647]]
|
||||||
|
//输出:[[2147483647]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:rooms = [[0]]
|
||||||
|
//输出:[[0]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// m == rooms.length
|
||||||
|
// n == rooms[i].length
|
||||||
|
// 1 <= m, n <= 250
|
||||||
|
// rooms[i][j] 是 -1、0 或 231 - 1
|
||||||
|
//
|
||||||
|
// Related Topics 广度优先搜索 数组 矩阵
|
||||||
|
// 👍 154 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
//286:墙与门
|
||||||
|
public class WallsAndGates {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new WallsAndGates().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
private static final int EMPTY = Integer.MAX_VALUE;
|
||||||
|
private static final int GATE = 0;
|
||||||
|
private final List<int[]> DIRECTIONS = Arrays.asList(
|
||||||
|
new int[]{1, 0},
|
||||||
|
new int[]{-1, 0},
|
||||||
|
new int[]{0, 1},
|
||||||
|
new int[]{0, -1}
|
||||||
|
);
|
||||||
|
|
||||||
|
public void wallsAndGates(int[][] rooms) {
|
||||||
|
int m = rooms.length;
|
||||||
|
if (m == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int n = rooms[0].length;
|
||||||
|
Queue<int[]> q = new LinkedList<>();
|
||||||
|
for (int row = 0; row < m; row++) {
|
||||||
|
for (int col = 0; col < n; col++) {
|
||||||
|
if (rooms[row][col] == GATE) {
|
||||||
|
q.add(new int[]{row, col});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (!q.isEmpty()) {
|
||||||
|
int[] point = q.poll();
|
||||||
|
int row = point[0];
|
||||||
|
int col = point[1];
|
||||||
|
for (int[] direction : DIRECTIONS) {
|
||||||
|
int r = row + direction[0];
|
||||||
|
int c = col + direction[1];
|
||||||
|
if (r < 0 || c < 0 || r >= m || c >= n || rooms[r][c] != EMPTY) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rooms[r][c] = rooms[row][col] + 1;
|
||||||
|
q.add(new int[]{r, c});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
51
src/main/java/leetcode/editor/cn/WallsAndGates.md
Normal file
51
src/main/java/leetcode/editor/cn/WallsAndGates.md
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<p>你被给定一个 <code>m × n</code> 的二维网格 <code>rooms</code> ,网格中有以下三种可能的初始化值:</p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li><code>-1</code> 表示墙或是障碍物</li>
|
||||||
|
<li><code>0</code> 表示一扇门</li>
|
||||||
|
<li><code>INF</code> 无限表示一个空的房间。然后,我们用 <code>2<sup>31</sup> - 1 = 2147483647</code> 代表 <code>INF</code>。你可以认为通往门的距离总是小于 <code>2147483647</code> 的。</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p>你要给每个空房间位上填上该房间到 <strong>最近门的距离</strong> ,如果无法到达门,则填 <code>INF</code> 即可。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/03/grid.jpg" style="width: 500px; height: 223px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
|
||||||
|
<strong>输出:</strong>[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rooms = [[-1]]
|
||||||
|
<strong>输出:</strong>[[-1]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rooms = [[2147483647]]
|
||||||
|
<strong>输出:</strong>[[2147483647]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>rooms = [[0]]
|
||||||
|
<strong>输出:</strong>[[0]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>m == rooms.length</code></li>
|
||||||
|
<li><code>n == rooms[i].length</code></li>
|
||||||
|
<li><code>1 <= m, n <= 250</code></li>
|
||||||
|
<li><code>rooms[i][j]</code> 是 <code>-1</code>、<code>0</code> 或 <code>2<sup>31</sup> - 1</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 154</li><li>👎 0</li></div>
|
93
src/main/java/leetcode/editor/cn/XuLieHuaErChaShuLcof.java
Normal file
93
src/main/java/leetcode/editor/cn/XuLieHuaErChaShuLcof.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
//请实现两个函数,分别用来序列化和反序列化二叉树。
|
||||||
|
//
|
||||||
|
// 你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字
|
||||||
|
//符串反序列化为原始的树结构。
|
||||||
|
//
|
||||||
|
// 提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方
|
||||||
|
//法解决这个问题。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:root = [1,2,3,null,null,4,5]
|
||||||
|
//输出:[1,2,3,null,null,4,5]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 注意:本题与主站 297 题相同:https://leetcode-cn.com/problems/serialize-and-deserialize-b
|
||||||
|
//inary-tree/
|
||||||
|
// Related Topics 树 深度优先搜索 广度优先搜索 设计 字符串 二叉树
|
||||||
|
// 👍 175 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
//剑指 Offer 37:序列化二叉树
|
||||||
|
public class XuLieHuaErChaShuLcof{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new XuLieHuaErChaShuLcof().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode(int x) { val = x; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public class Codec {
|
||||||
|
|
||||||
|
// Encodes a tree to a single string.
|
||||||
|
public String serialize(TreeNode root) {
|
||||||
|
String result = "";
|
||||||
|
if (root == null) {
|
||||||
|
return "null,";
|
||||||
|
}
|
||||||
|
result += root.val + ",";
|
||||||
|
result += serialize(root.left);
|
||||||
|
result += serialize(root.right);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decodes your encoded data to tree.
|
||||||
|
public TreeNode deserialize(String data) {
|
||||||
|
if ("null,".equals(data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String[] strs = data.split(",");
|
||||||
|
Queue<String> queue = new LinkedList<>();
|
||||||
|
for (String str : strs) {
|
||||||
|
queue.offer(str);
|
||||||
|
}
|
||||||
|
return des(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TreeNode des(Queue<String> queue) {
|
||||||
|
String value = queue.poll();
|
||||||
|
if ("null".equals(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode root = new TreeNode(Integer.parseInt(value));
|
||||||
|
root.left = des(queue);
|
||||||
|
root.right = des(queue);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Your Codec object will be instantiated and called as such:
|
||||||
|
// Codec codec = new Codec();
|
||||||
|
// codec.deserialize(codec.serialize(root));
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
19
src/main/java/leetcode/editor/cn/XuLieHuaErChaShuLcof.md
Normal file
19
src/main/java/leetcode/editor/cn/XuLieHuaErChaShuLcof.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<p>请实现两个函数,分别用来序列化和反序列化二叉树。</p>
|
||||||
|
|
||||||
|
<p>你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
|
||||||
|
<strong>输出:</strong>[1,2,3,null,null,4,5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p>注意:本题与主站 297 题相同:<a href="https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/">https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/</a></p>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>设计</li><li>字符串</li><li>二叉树</li></div></div>\n<div><li>👍 175</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user