Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
commit
a14d0ae471
@ -9,6 +9,8 @@ public class Node {
|
||||
public Node random;
|
||||
public Node prev;
|
||||
public Node child;
|
||||
public Node left;
|
||||
public Node right;
|
||||
public List<Node> children;
|
||||
|
||||
public Node(int val) {
|
||||
|
69
src/main/java/leetcode/editor/cn/BeautifulArrangement.java
Normal file
69
src/main/java/leetcode/editor/cn/BeautifulArrangement.java
Normal file
@ -0,0 +1,69 @@
|
||||
//假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,
|
||||
//我们就称这个数组为一个优美的排列。条件:
|
||||
//
|
||||
//
|
||||
// 第 i 位的数字能被 i 整除
|
||||
// i 能被第 i 位上的数字整除
|
||||
//
|
||||
//
|
||||
// 现在给定一个整数 N,请问可以构造多少个优美的排列?
|
||||
//
|
||||
// 示例1:
|
||||
//
|
||||
//
|
||||
//输入: 2
|
||||
//输出: 2
|
||||
//解释:
|
||||
//
|
||||
//第 1 个优美的排列是 [1, 2]:
|
||||
// 第 1 个位置(i=1)上的数字是1,1能被 i(i=1)整除
|
||||
// 第 2 个位置(i=2)上的数字是2,2能被 i(i=2)整除
|
||||
//
|
||||
//第 2 个优美的排列是 [2, 1]:
|
||||
// 第 1 个位置(i=1)上的数字是2,2能被 i(i=1)整除
|
||||
// 第 2 个位置(i=2)上的数字是1,i(i=2)能被 1 整除
|
||||
//
|
||||
//
|
||||
// 说明:
|
||||
//
|
||||
//
|
||||
// N 是一个正整数,并且不会超过15。
|
||||
//
|
||||
// Related Topics 位运算 数组 动态规划 回溯 状态压缩
|
||||
// 👍 194 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//526:优美的排列
|
||||
class BeautifulArrangement {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BeautifulArrangement().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int countArrangement(int n) {
|
||||
return dfs(n, 1, new boolean[n]);
|
||||
}
|
||||
|
||||
private int dfs(int n, int index, boolean[] use) {
|
||||
if (index > n) {
|
||||
return 1;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (use[i - 1] || (i % index > 0 && index % i > 0)) {
|
||||
continue;
|
||||
}
|
||||
use[i - 1] = true;
|
||||
count += dfs(n, index + 1, use);
|
||||
use[i - 1] = false;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
31
src/main/java/leetcode/editor/cn/BeautifulArrangement.md
Normal file
31
src/main/java/leetcode/editor/cn/BeautifulArrangement.md
Normal file
@ -0,0 +1,31 @@
|
||||
<p>假设有从 1 到 N 的 <strong>N </strong>个整数,如果从这 <strong>N </strong>个数字中成功构造出一个数组,使得数组的第 <strong>i</strong> 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这个数组为一个优美的排列。条件:</p>
|
||||
|
||||
<ol>
|
||||
<li>第 <strong>i </strong>位的数字能被 <strong>i </strong>整除</li>
|
||||
<li><strong>i</strong> 能被第 <strong>i</strong> 位上的数字整除</li>
|
||||
</ol>
|
||||
|
||||
<p>现在给定一个整数 N,请问可以构造多少个优美的排列?</p>
|
||||
|
||||
<p><strong>示例1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> 2
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong>
|
||||
|
||||
第 1 个优美的排列是 [1, 2]:
|
||||
第 1 个位置(i=1)上的数字是1,1能被 i(i=1)整除
|
||||
第 2 个位置(i=2)上的数字是2,2能被 i(i=2)整除
|
||||
|
||||
第 2 个优美的排列是 [2, 1]:
|
||||
第 1 个位置(i=1)上的数字是2,2能被 i(i=1)整除
|
||||
第 2 个位置(i=2)上的数字是1,i(i=2)能被 1 整除
|
||||
</pre>
|
||||
|
||||
<p><strong>说明:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><strong>N</strong> 是一个正整数,并且不会超过15。</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li><li>动态规划</li><li>回溯</li><li>状态压缩</li></div></div>\n<div><li>👍 194</li><li>👎 0</li></div>
|
63
src/main/java/leetcode/editor/cn/CustomSortString.java
Normal file
63
src/main/java/leetcode/editor/cn/CustomSortString.java
Normal file
@ -0,0 +1,63 @@
|
||||
//字符串S和 T 只包含小写字符。在S中,所有字符只会出现一次。
|
||||
//
|
||||
// S 已经根据某种规则进行了排序。我们要根据S中的字符顺序对T进行排序。更具体地说,如果S中x在y之前出现,那么返回的字符串中x也应出现在y之前。
|
||||
//
|
||||
// 返回任意一种符合条件的字符串T。
|
||||
//
|
||||
//
|
||||
//示例:
|
||||
//输入:
|
||||
//S = "cba"
|
||||
//T = "abcd"
|
||||
//输出: "cbad"
|
||||
//解释:
|
||||
//S中出现了字符 "a", "b", "c", 所以 "a", "b", "c" 的顺序应该是 "c", "b", "a".
|
||||
//由于 "d" 没有在S中出现, 它可以放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的输出。
|
||||
//
|
||||
//
|
||||
// 注意:
|
||||
//
|
||||
//
|
||||
// S的最大长度为26,其中没有重复的字符。
|
||||
// T的最大长度为200。
|
||||
// S和T只包含小写字符。
|
||||
//
|
||||
// Related Topics 哈希表 字符串 排序
|
||||
// 👍 83 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//791:自定义字符串排序
|
||||
class CustomSortString {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CustomSortString().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String customSortString(String order, String s) {
|
||||
int[] arr = new int[26];
|
||||
for (char ch : s.toCharArray()) {
|
||||
arr[ch - 'a']++;
|
||||
}
|
||||
s = "";
|
||||
for (char ch : order.toCharArray()) {
|
||||
int size = arr[ch - 'a'];
|
||||
for (int i = 0; i < size; i++) {
|
||||
s += ch;
|
||||
arr[ch - 'a']--;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 26; i++) {
|
||||
for (int j = 0; j < arr[i]; j++) {
|
||||
s += ('a' + i);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
25
src/main/java/leetcode/editor/cn/CustomSortString.md
Normal file
25
src/main/java/leetcode/editor/cn/CustomSortString.md
Normal file
@ -0,0 +1,25 @@
|
||||
<p>字符串<code>S</code>和 <code>T</code> 只包含小写字符。在<code>S</code>中,所有字符只会出现一次。</p>
|
||||
|
||||
<p><code>S</code> 已经根据某种规则进行了排序。我们要根据<code>S</code>中的字符顺序对<code>T</code>进行排序。更具体地说,如果<code>S</code>中<code>x</code>在<code>y</code>之前出现,那么返回的字符串中<code>x</code>也应出现在<code>y</code>之前。</p>
|
||||
|
||||
<p>返回任意一种符合条件的字符串<code>T</code>。</p>
|
||||
|
||||
<pre>
|
||||
<strong>示例:</strong>
|
||||
<strong>输入:</strong>
|
||||
S = "cba"
|
||||
T = "abcd"
|
||||
<strong>输出:</strong> "cbad"
|
||||
<strong>解释:</strong>
|
||||
S中出现了字符 "a", "b", "c", 所以 "a", "b", "c" 的顺序应该是 "c", "b", "a".
|
||||
由于 "d" 没有在S中出现, 它可以放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的输出。
|
||||
</pre>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>S</code>的最大长度为<code>26</code>,其中没有重复的字符。</li>
|
||||
<li><code>T</code>的最大长度为<code>200</code>。</li>
|
||||
<li><code>S</code>和<code>T</code>只包含小写字符。</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>排序</li></div></div>\n<div><li>👍 83</li><li>👎 0</li></div>
|
@ -0,0 +1,79 @@
|
||||
//给你一个整数数组 nums,每次 操作 会从中选择一个元素并 将该元素的值减少 1。
|
||||
//
|
||||
// 如果符合下列情况之一,则数组 A 就是 锯齿数组:
|
||||
//
|
||||
//
|
||||
// 每个偶数索引对应的元素都大于相邻的元素,即 A[0] > A[1] < A[2] > A[3] < A[4] > ...
|
||||
// 或者,每个奇数索引对应的元素都大于相邻的元素,即 A[0] < A[1] > A[2] < A[3] > A[4] < ...
|
||||
//
|
||||
//
|
||||
// 返回将数组 nums 转换为锯齿数组所需的最小操作次数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:nums = [1,2,3]
|
||||
//输出:2
|
||||
//解释:我们可以把 2 递减到 0,或把 3 递减到 1。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:nums = [9,6,1,6,2]
|
||||
//输出:4
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 1000
|
||||
// 1 <= nums[i] <= 1000
|
||||
//
|
||||
// Related Topics 贪心 数组
|
||||
// 👍 33 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1144:递减元素使数组呈锯齿状
|
||||
class DecreaseElementsToMakeArrayZigzag {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new DecreaseElementsToMakeArrayZigzag().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int movesToMakeZigzag(int[] nums) {
|
||||
int count1 = 0;
|
||||
int count2 = 0;
|
||||
int[] arr = new int[nums.length];
|
||||
System.arraycopy(nums, 0, arr, 0, nums.length);
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (i % 2 == 0) {
|
||||
if (arr[i] <= arr[i - 1]) {
|
||||
count1 += arr[i - 1] - arr[i] + 1;
|
||||
}
|
||||
if (nums[i] >= nums[i - 1]) {
|
||||
count2 += nums[i] - nums[i - 1] + 1;
|
||||
}
|
||||
} else {
|
||||
if (nums[i] <= nums[i - 1]) {
|
||||
count2 += nums[i - 1] - nums[i] + 1;
|
||||
nums[i] = nums[i - 1] - 1;
|
||||
}
|
||||
if (arr[i] >= arr[i - 1]) {
|
||||
count1 += arr[i] - arr[i - 1] + 1;
|
||||
arr[i] = arr[i - 1] - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Math.min(count1, count2);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个整数数组 <code>nums</code>,每次 <strong>操作</strong> 会从中选择一个元素并 <strong>将该元素的值减少 1</strong>。</p>
|
||||
|
||||
<p>如果符合下列情况之一,则数组 <code>A</code> 就是 <strong>锯齿数组</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个偶数索引对应的元素都大于相邻的元素,即 <code>A[0] > A[1] < A[2] > A[3] < A[4] > ...</code></li>
|
||||
<li>或者,每个奇数索引对应的元素都大于相邻的元素,即 <code>A[0] < A[1] > A[2] < A[3] > A[4] < ...</code></li>
|
||||
</ul>
|
||||
|
||||
<p>返回将数组 <code>nums</code> 转换为锯齿数组所需的最小操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>我们可以把 2 递减到 0,或把 3 递减到 1。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [9,6,1,6,2]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li></div></div>\n<div><li>👍 33</li><li>👎 0</li></div>
|
92
src/main/java/leetcode/editor/cn/NumberOfProvinces.java
Normal file
92
src/main/java/leetcode/editor/cn/NumberOfProvinces.java
Normal file
@ -0,0 +1,92 @@
|
||||
//
|
||||
//
|
||||
// 有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连
|
||||
//。
|
||||
//
|
||||
// 省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。
|
||||
//
|
||||
// 给你一个 n x n 的矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i 个城市和第 j 个城市直接相连,而
|
||||
//isConnected[i][j] = 0 表示二者不直接相连。
|
||||
//
|
||||
// 返回矩阵中 省份 的数量。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:isConnected = [[1,1,0],[1,1,0],[0,0,1]]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:isConnected = [[1,0,0],[0,1,0],[0,0,1]]
|
||||
//输出:3
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= n <= 200
|
||||
// n == isConnected.length
|
||||
// n == isConnected[i].length
|
||||
// isConnected[i][j] 为 1 或 0
|
||||
// isConnected[i][i] == 1
|
||||
// isConnected[i][j] == isConnected[j][i]
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 深度优先搜索 广度优先搜索 并查集 图
|
||||
// 👍 595 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
//547:省份数量
|
||||
class NumberOfProvinces {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new NumberOfProvinces().new Solution();
|
||||
TwoArray twoArray = new TwoArray("[[1,1,0],[1,1,0],[0,0,1]]", true);
|
||||
System.out.println(solution.findCircleNum(twoArray.getArr()));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int findCircleNum(int[][] isConnected) {
|
||||
int length = isConnected.length;
|
||||
boolean[] use = new boolean[length];
|
||||
|
||||
int count = 0;
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (!use[i]) {
|
||||
count++;
|
||||
queue.add(i);
|
||||
use[i] = true;
|
||||
while (!queue.isEmpty()) {
|
||||
int num = queue.poll();
|
||||
for (int j = 0; j < length; j++) {
|
||||
if (isConnected[num][j] == 1 && !use[j]) {
|
||||
use[j] = true;
|
||||
queue.add(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
41
src/main/java/leetcode/editor/cn/NumberOfProvinces.md
Normal file
41
src/main/java/leetcode/editor/cn/NumberOfProvinces.md
Normal file
@ -0,0 +1,41 @@
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p>有 <code>n</code> 个城市,其中一些彼此相连,另一些没有相连。如果城市 <code>a</code> 与城市 <code>b</code> 直接相连,且城市 <code>b</code> 与城市 <code>c</code> 直接相连,那么城市 <code>a</code> 与城市 <code>c</code> 间接相连。</p>
|
||||
|
||||
<p><strong>省份</strong> 是一组直接或间接相连的城市,组内不含其他没有相连的城市。</p>
|
||||
|
||||
<p>给你一个 <code>n x n</code> 的矩阵 <code>isConnected</code> ,其中 <code>isConnected[i][j] = 1</code> 表示第 <code>i</code> 个城市和第 <code>j</code> 个城市直接相连,而 <code>isConnected[i][j] = 0</code> 表示二者不直接相连。</p>
|
||||
|
||||
<p>返回矩阵中 <strong>省份</strong> 的数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>isConnected = [[1,1,0],[1,1,0],[0,0,1]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>isConnected = [[1,0,0],[0,1,0],[0,0,1]]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>n == isConnected.length</code></li>
|
||||
<li><code>n == isConnected[i].length</code></li>
|
||||
<li><code>isConnected[i][j]</code> 为 <code>1</code> 或 <code>0</code></li>
|
||||
<li><code>isConnected[i][i] == 1</code></li>
|
||||
<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li>图</li></div></div>\n<div><li>👍 595</li><li>👎 0</li></div>
|
@ -0,0 +1,116 @@
|
||||
//给定一个二叉树
|
||||
//
|
||||
//
|
||||
//struct Node {
|
||||
// int val;
|
||||
// Node *left;
|
||||
// Node *right;
|
||||
// Node *next;
|
||||
//}
|
||||
//
|
||||
// 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
|
||||
//
|
||||
// 初始状态下,所有 next 指针都被设置为 NULL。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:
|
||||
//
|
||||
//
|
||||
// 你只能使用常量级额外空间。
|
||||
// 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:root = [1,2,3,4,5,null,7]
|
||||
//输出:[1,#,2,3,#,4,5,7,#]
|
||||
//解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指
|
||||
//针连接),'#' 表示每层的末尾。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 树中的节点数小于 6000
|
||||
// -100 <= node.val <= 100
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树
|
||||
// 👍 436 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.Node;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
//117:填充每个节点的下一个右侧节点指针 II
|
||||
class PopulatingNextRightPointersInEachNodeIi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new PopulatingNextRightPointersInEachNodeIi().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/*
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
public int val;
|
||||
public Node left;
|
||||
public Node right;
|
||||
public Node next;
|
||||
|
||||
public Node() {}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, Node _left, Node _right, Node _next) {
|
||||
val = _val;
|
||||
left = _left;
|
||||
right = _right;
|
||||
next = _next;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public Node connect(Node root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
Queue<Node> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Node node = queue.poll();
|
||||
node.next = i == size - 1 ? null : queue.peek();
|
||||
if (node.left != null) {
|
||||
queue.add(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
queue.add(node.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
<p>给定一个二叉树</p>
|
||||
|
||||
<pre>
|
||||
struct Node {
|
||||
int val;
|
||||
Node *left;
|
||||
Node *right;
|
||||
Node *next;
|
||||
}</pre>
|
||||
|
||||
<p>填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 <code>NULL</code>。</p>
|
||||
|
||||
<p>初始状态下,所有 next 指针都被设置为 <code>NULL</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>你只能使用常量级额外空间。</li>
|
||||
<li>使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/15/117_sample.png" style="height: 218px; width: 640px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>:root = [1,2,3,4,5,null,7]
|
||||
<strong>输出:</strong>[1,#,2,3,#,4,5,7,#]
|
||||
<strong>解释:</strong>给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数小于 <code>6000</code></li>
|
||||
<li><code>-100 <= node.val <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 436</li><li>👎 0</li></div>
|
@ -0,0 +1,80 @@
|
||||
//给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
|
||||
//
|
||||
//
|
||||
// 'A':Absent,缺勤
|
||||
// 'L':Late,迟到
|
||||
// 'P':Present,到场
|
||||
//
|
||||
//
|
||||
// 如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
|
||||
//
|
||||
//
|
||||
// 按 总出勤 计,学生缺勤('A')严格 少于两天。
|
||||
// 学生 不会 存在 连续 3 天或 3 天以上的迟到('L')记录。
|
||||
//
|
||||
//
|
||||
// 如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:s = "PPALLP"
|
||||
//输出:true
|
||||
//解释:学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:s = "PPALLL"
|
||||
//输出:false
|
||||
//解释:学生最后三天连续迟到,所以不满足出勤奖励的条件。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 1000
|
||||
// s[i] 为 'A'、'L' 或 'P'
|
||||
//
|
||||
// Related Topics 字符串
|
||||
// 👍 99 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//551:学生出勤记录 I
|
||||
class StudentAttendanceRecordI {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new StudentAttendanceRecordI().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean checkRecord(String s) {
|
||||
int ca = 0;
|
||||
int cl = 0;
|
||||
for (char ch : s.toCharArray()) {
|
||||
if (ch == 'A') {
|
||||
ca++;
|
||||
}
|
||||
if (ch == 'L') {
|
||||
cl++;
|
||||
} else {
|
||||
cl = 0;
|
||||
}
|
||||
if (ca == 2 || cl == 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
44
src/main/java/leetcode/editor/cn/StudentAttendanceRecordI.md
Normal file
44
src/main/java/leetcode/editor/cn/StudentAttendanceRecordI.md
Normal file
@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串 <code>s</code> 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'A'</code>:Absent,缺勤</li>
|
||||
<li><code>'L'</code>:Late,迟到</li>
|
||||
<li><code>'P'</code>:Present,到场</li>
|
||||
</ul>
|
||||
|
||||
<p>如果学生能够 <strong>同时</strong> 满足下面两个条件,则可以获得出勤奖励:</p>
|
||||
|
||||
<ul>
|
||||
<li>按 <strong>总出勤</strong> 计,学生缺勤(<code>'A'</code>)<strong>严格</strong> 少于两天。</li>
|
||||
<li>学生 <strong>不会</strong> 存在 <strong>连续</strong> 3 天或 3 天以上的迟到(<code>'L'</code>)记录。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果学生可以获得出勤奖励,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "PPALLP"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "PPALLL"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>学生最后三天连续迟到,所以不满足出勤奖励的条件。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> 为 <code>'A'</code>、<code>'L'</code> 或 <code>'P'</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 99</li><li>👎 0</li></div>
|
101
src/main/java/leetcode/editor/cn/StudentAttendanceRecordIi.java
Normal file
101
src/main/java/leetcode/editor/cn/StudentAttendanceRecordIi.java
Normal file
@ -0,0 +1,101 @@
|
||||
//可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
|
||||
//
|
||||
// 'A':Absent,缺勤
|
||||
// 'L':Late,迟到
|
||||
// 'P':Present,到场
|
||||
//
|
||||
//
|
||||
// 如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
|
||||
//
|
||||
//
|
||||
// 按 总出勤 计,学生缺勤('A')严格 少于两天。
|
||||
// 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到('L')记录。
|
||||
//
|
||||
//
|
||||
// 给你一个整数 n ,表示出勤记录的长度(次数)。请你返回记录长度为 n 时,可能获得出勤奖励的记录情况 数量 。答案可能很大,所以返回对 109 + 7
|
||||
//取余 的结果。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:n = 2
|
||||
//输出:8
|
||||
//解释:
|
||||
//有 8 种长度为 2 的记录将被视为可奖励:
|
||||
//"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
|
||||
//只有"AA"不会被视为可奖励,因为缺勤次数为 2 次(需要少于 2 次)。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:n = 1
|
||||
//输出:3
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:n = 10101
|
||||
//输出:183236316
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= n <= 105
|
||||
//
|
||||
// Related Topics 动态规划
|
||||
// 👍 192 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//552:学生出勤记录 II
|
||||
class StudentAttendanceRecordIi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new StudentAttendanceRecordIi().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int checkRecord(int n) {
|
||||
int mod = 1000000007;
|
||||
int[][][] dp = new int[n + 1][2][3];
|
||||
dp[0][0][0] = 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 0; j <= 1; j++) {
|
||||
for (int k = 0; k <= 2; k++) {
|
||||
dp[i][j][0] += dp[i - 1][j][k];
|
||||
dp[i][j][0] %= mod;
|
||||
}
|
||||
}
|
||||
for (int k = 0; k <= 2; k++) {
|
||||
dp[i][1][0] += dp[i - 1][0][k];
|
||||
dp[i][1][0] %= mod;
|
||||
}
|
||||
for (int j = 0; j <= 1; j++) {
|
||||
for (int k = 1; k <= 2; k++) {
|
||||
dp[i][j][k] += dp[i - 1][j][k - 1];
|
||||
dp[i][j][k] %= mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
int sum = 0;
|
||||
for (int j = 0; j <= 1; j++) {
|
||||
for (int k = 0; k <= 2; k++) {
|
||||
sum += dp[n][j][k];
|
||||
sum %= mod;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
|
||||
<ul>
|
||||
<li><code>'A'</code>:Absent,缺勤</li>
|
||||
<li><code>'L'</code>:Late,迟到</li>
|
||||
<li><code>'P'</code>:Present,到场</li>
|
||||
</ul>
|
||||
|
||||
<p>如果学生能够 <strong>同时</strong> 满足下面两个条件,则可以获得出勤奖励:</p>
|
||||
|
||||
<ul>
|
||||
<li>按 <strong>总出勤</strong> 计,学生缺勤(<code>'A'</code>)<strong>严格</strong> 少于两天。</li>
|
||||
<li>学生 <strong>不会</strong> 存在 <strong>连续</strong> 3 天或 <strong>连续</strong> 3 天以上的迟到(<code>'L'</code>)记录。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数 <code>n</code> ,表示出勤记录的长度(次数)。请你返回记录长度为 <code>n</code> 时,可能获得出勤奖励的记录情况 <strong>数量</strong> 。答案可能很大,所以返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:
|
||||
</strong>有 8 种长度为 2 的记录将被视为可奖励:
|
||||
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
|
||||
只有"AA"不会被视为可奖励,因为缺勤次数为 2 次(需要少于 2 次)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 10101
|
||||
<strong>输出:</strong>183236316
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>动态规划</li></div></div>\n<div><li>👍 192</li><li>👎 0</li></div>
|
89
src/main/java/leetcode/editor/cn/SubtreeOfAnotherTree.java
Normal file
89
src/main/java/leetcode/editor/cn/SubtreeOfAnotherTree.java
Normal file
@ -0,0 +1,89 @@
|
||||
//
|
||||
//
|
||||
// 给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。如果存在,返回 true ;否则
|
||||
//,返回 false 。
|
||||
//
|
||||
// 二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:root = [3,4,5,1,2], subRoot = [4,1,2]
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// root 树上的节点数量范围是 [1, 2000]
|
||||
// subRoot 树上的节点数量范围是 [1, 1000]
|
||||
// -104 <= root.val <= 104
|
||||
// -104 <= subRoot.val <= 104
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 树 深度优先搜索 二叉树 字符串匹配 哈希函数
|
||||
// 👍 539 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//572:另一棵树的子树
|
||||
class SubtreeOfAnotherTree {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SubtreeOfAnotherTree().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 isSubtree(TreeNode root, TreeNode subRoot) {
|
||||
if (root == null) {
|
||||
return false;
|
||||
}
|
||||
return check(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
|
||||
}
|
||||
|
||||
public boolean check(TreeNode root, TreeNode subRoot) {
|
||||
if (root == null && subRoot == null) {
|
||||
return true;
|
||||
}
|
||||
if (root == null || subRoot == null || root.val != subRoot.val) {
|
||||
return false;
|
||||
}
|
||||
return check(root.left, subRoot.left) && check(root.right, subRoot.right);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
35
src/main/java/leetcode/editor/cn/SubtreeOfAnotherTree.md
Normal file
35
src/main/java/leetcode/editor/cn/SubtreeOfAnotherTree.md
Normal file
@ -0,0 +1,35 @@
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p>给你两棵二叉树 <code>root</code> 和 <code>subRoot</code> 。检验 <code>root</code> 中是否包含和 <code>subRoot</code> 具有相同结构和节点值的子树。如果存在,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p>二叉树 <code>tree</code> 的一棵子树包括 <code>tree</code> 的某个节点和这个节点的所有后代节点。<code>tree</code> 也可以看做它自身的一棵子树。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree1-tree.jpg" style="width: 532px; height: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,4,5,1,2], subRoot = [4,1,2]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/subtree2-tree.jpg" style="width: 502px; height: 458px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>root</code> 树上的节点数量范围是 <code>[1, 2000]</code></li>
|
||||
<li><code>subRoot</code> 树上的节点数量范围是 <code>[1, 1000]</code></li>
|
||||
<li><code>-10<sup>4</sup> <= root.val <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= subRoot.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>二叉树</li><li>字符串匹配</li><li>哈希函数</li></div></div>\n<div><li>👍 539</li><li>👎 0</li></div>
|
104
src/main/java/leetcode/editor/cn/SurroundedRegions.java
Normal file
104
src/main/java/leetcode/editor/cn/SurroundedRegions.java
Normal file
@ -0,0 +1,104 @@
|
||||
//给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充
|
||||
//。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X"
|
||||
//,"X"]]
|
||||
//输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
|
||||
//解释:被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都
|
||||
//会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:board = [["X"]]
|
||||
//输出:[["X"]]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// m == board.length
|
||||
// n == board[i].length
|
||||
// 1 <= m, n <= 200
|
||||
// board[i][j] 为 'X' 或 'O'
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 深度优先搜索 广度优先搜索 并查集 数组 矩阵
|
||||
// 👍 587 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
//130:被围绕的区域
|
||||
class SurroundedRegions {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SurroundedRegions().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public void solve(char[][] board) {
|
||||
boolean[][] use = new boolean[board.length][board[0].length];
|
||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
if (board[i][0] == 'O') {
|
||||
queue.add(new Pair<>(i, 0));
|
||||
use[i][0] = true;
|
||||
}
|
||||
if (board[i][board[0].length - 1] == 'O') {
|
||||
queue.add(new Pair<>(i, board[0].length - 1));
|
||||
use[i][board[0].length - 1] = true;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i < board[0].length - 1; i++) {
|
||||
if (board[0][i] == 'O') {
|
||||
queue.add(new Pair<>(0, i));
|
||||
use[0][i] = true;
|
||||
}
|
||||
if (board[board.length - 1][i] == 'O') {
|
||||
queue.add(new Pair<>(board.length - 1, i));
|
||||
use[board.length - 1][i] = true;
|
||||
}
|
||||
}
|
||||
int[] xIndex = new int[]{-1, 1, 0, 0};
|
||||
int[] yIndex = new int[]{0, 0, -1, 1};
|
||||
while (!queue.isEmpty()) {
|
||||
Pair<Integer, Integer> pair = queue.poll();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int x = pair.getKey() + xIndex[i];
|
||||
int y = pair.getValue() + yIndex[i];
|
||||
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || use[x][y] || board[x][y] == 'X') {
|
||||
continue;
|
||||
}
|
||||
queue.add(new Pair<>(x, y));
|
||||
use[x][y] = true;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[0].length; j++) {
|
||||
if (board[i][j] == 'O' && !use[i][j]) {
|
||||
board[i][j] = 'X';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
33
src/main/java/leetcode/editor/cn/SurroundedRegions.md
Normal file
33
src/main/java/leetcode/editor/cn/SurroundedRegions.md
Normal file
@ -0,0 +1,33 @@
|
||||
给你一个 <code>m x n</code> 的矩阵 <code>board</code> ,由若干字符 <code>'X'</code> 和 <code>'O'</code> ,找到所有被 <code>'X'</code> 围绕的区域,并将这些区域里所有的 <code>'O'</code> 用 <code>'X'</code> 填充。
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg" style="width: 550px; height: 237px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
|
||||
<strong>输出:</strong>[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
|
||||
<strong>解释:</strong>被围绕的区间不会存在于边界上,换句话说,任何边界上的 <code>'O'</code> 都不会被填充为 <code>'X'</code>。 任何不在边界上,或不与边界上的 <code>'O'</code> 相连的 <code>'O'</code> 最终都会被填充为 <code>'X'</code>。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["X"]]
|
||||
<strong>输出:</strong>[["X"]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n == board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>board[i][j]</code> 为 <code>'X'</code> 或 <code>'O'</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li>并查集</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 587</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user