hpstory

4445

BT7274
WangJY

acwing_gza
7u6in9

Lukesu
ye_zhui_yi
PersiusEGO

qwertyuiop4000
Renaissance_0
QAQwhq
YJHigher
im0use

hpstory
15分钟前

#### C# 代码

public class Solution {
public IList<IList<int>> ReconstructMatrix(int upper, int lower, int[] colsum) {
int n = colsum.Length;
int[] up = new int[n], down = new int[n];
List<IList<int>> result = new List<IList<int>>();
for (int i = 0; i < n; i++){
if (colsum[i] == 2){
up[i] = 1;
down[i] = 1;
upper--;
lower--;
}
}

for (int j = 0; j < n; j++){
if (colsum[j] == 1){
if (upper > 0){
up[j] = 1;
upper--;
}
else if (lower > 0){
down[j] = 1;
lower--;
}
else{
return result;
}
}
}

if (upper == 0 && lower == 0){
}

return result;
}
}


hpstory
15分钟前
public class Solution {
public IList<IList<int>> ReconstructMatrix(int upper, int lower, int[] colsum) {
int n = colsum.Length;
int[] up = new int[n], down = new int[n];
List<IList<int>> result = new List<IList<int>>();
for (int i = 0; i < n; i++){
if (colsum[i] == 2){
up[i] = 1;
down[i] = 1;
upper--;
lower--;
}
}

for (int j = 0; j < n; j++){
if (colsum[j] == 1){
if (upper > 0){
up[j] = 1;
upper--;
}
else if (lower > 0){
down[j] = 1;
lower--;
}
else{
return result;
}
}
}

if (upper == 0 && lower == 0){
}

return result;
}
}


hpstory
39分钟前

#### C# DFS 代码

public class Solution {
private bool island;
public int ClosedIsland(int[][] grid) {
int result = 0;
for (int i = 0; i < grid.Length; i++){
for (int j = 0; j < grid[0].Length; j++){
if (grid[i][j] == 0){
island = true;
DFS(i, j, grid);
if (island){
result++;
}
}
}
}

return result;
}

private void DFS(int i, int j, int[][] grid){
if (i < 0 || i >= grid.Length || j < 0 || j >= grid[0].Length){
island = false;
return;
}

if (grid[i][j] == 1) return;
grid[i][j] = 1;
DFS(i, j + 1, grid);
DFS(i + 1, j, grid);
DFS(i - 1, j, grid);
DFS(i, j - 1, grid);
}
}


#### C# BFS 代码

public class Solution {
private int[][] dirs = new int[4][]{
new int[]{0,1},
new int[]{1,0},
new int[]{-1,0},
new int[]{0,-1}
};

private Queue<int[]> queue = new Queue<int[]>();
public int ClosedIsland(int[][] grid) {
int result = 0;
for (int i = 0; i < grid.Length; i++){
for (int j = 0; j < grid[0].Length; j++){
if (grid[i][j] == 0){
queue.Enqueue(new int[]{i, j});
if (BFS(grid)) result++;
}
}
}

return result;
}

private bool BFS(int[][] grid){
bool island = true;
while (queue.Count > 0){
int[] land = queue.Dequeue();
foreach (int[] dir in dirs){
int r = land[0] + dir[0];
int c = land[1] + dir[1];
if (r < 0 || r >= grid.Length || c < 0 || c >= grid[0].Length){
// 遇到边界, 表示当前整个连通块都不行, 但是仍需要全部置1
island = false;
continue;
}

if (grid[r][c] == 0){
grid[r][c] = 1;
queue.Enqueue(new int[]{r,c});
}
}
}

return island;
}
}


hpstory
1小时前
public class Solution {
private int[][] dirs = new int[4][]{
new int[]{0,1},
new int[]{1,0},
new int[]{-1,0},
new int[]{0,-1}
};

private Queue<int[]> queue = new Queue<int[]>();
public int ClosedIsland(int[][] grid) {
int result = 0;
for (int i = 0; i < grid.Length; i++){
for (int j = 0; j < grid[0].Length; j++){
if (grid[i][j] == 0){
queue.Enqueue(new int[]{i, j});
if (BFS(grid)) result++;
}
}
}

return result;
}

private bool BFS(int[][] grid){
bool island = true;
while (queue.Count > 0){
int[] land = queue.Dequeue();
foreach (int[] dir in dirs){
int r = land[0] + dir[0];
int c = land[1] + dir[1];
if (r < 0 || r >= grid.Length || c < 0 || c >= grid[0].Length){
island = false;
continue;
}

if (grid[r][c] == 0){
grid[r][c] = 1;
queue.Enqueue(new int[]{r,c});
}
}
}

return island;
}
}


hpstory
2小时前

#### C# BFS代码

public class FindElements {
private Queue<TreeNode> queue = new Queue<TreeNode>();
private HashSet<int> set = new HashSet<int>();
public FindElements(TreeNode root) {
root.val = 0;
queue.Enqueue(root);
while (queue.Count > 0){
TreeNode current = queue.Dequeue();
if (current.left != null){
current.left.val = (current.val << 1) + 1;
queue.Enqueue(current.left);
}

if (current.right != null){
current.right.val = (current.val << 1) + 2;
queue.Enqueue(current.right);
}
}
}

public bool Find(int target) {
return set.Contains(target);
}
}


#### C# DFS代码

public class FindElements {
private HashSet<int> set = new HashSet<int>();
public FindElements(TreeNode root) {
root.val = 0;
DFS(root, 0);
}

private void DFS(TreeNode node, int value){
if (node.left != null){
DFS(node.left, 1 + (value << 1));
}

if (node.right != null){
DFS(node.right, 2 + (value << 1));
}
}

public bool Find(int target) {
return set.Contains(target);
}
}


hpstory
2小时前
public class FindElements {
private Queue<TreeNode> queue = new Queue<TreeNode>();
private HashSet<int> set = new HashSet<int>();
public FindElements(TreeNode root) {
root.val = 0;
queue.Enqueue(root);
while (queue.Count > 0){
TreeNode current = queue.Dequeue();
if (current.left != null){
current.left.val = (current.val << 1) + 1;
queue.Enqueue(current.left);
}

if (current.right != null){
current.right.val = (current.val << 1) + 2;
queue.Enqueue(current.right);
}
}
}

public bool Find(int target) {
return set.Contains(target);
}
}


hpstory
9小时前

#### C# 代码

public class Solution {

while (fast != null && fast.next != null){
fast = fast.next;
slow = slow.next;
if (fast != null){
fast = fast.next;
}

if (fast == slow){
while (fast != slow){
fast = fast.next;
slow = slow.next;
}

return slow;
}
}

return null;
}
}


hpstory
9小时前
/**
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {

while (fast != null && fast.next != null){
fast = fast.next;
slow = slow.next;
if (fast != null){
fast = fast.next;
}

if (fast == slow){
while (fast != slow){
fast = fast.next;
slow = slow.next;
}

return slow;
}
}

return null;
}
}


hpstory
13小时前

#### C# 代码

public class Solution {
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}

return slow;
}
}


hpstory
13小时前
public class Solution {
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}

return slow;
}
}