头像

hpstory

专注C++++




在线 


最近来访(51)
用户头像
是逐梦吖
用户头像
听风于等你
用户头像
冰之韵
用户头像
BT7274
用户头像
WangJY
用户头像
航_0
用户头像
acwing_gza
用户头像
7u6in9
用户头像
走路撞墙的robot
用户头像
Lukesu
用户头像
ye_zhui_yi
用户头像
PersiusEGO
用户头像
呆@_9
用户头像
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){
            result.Add(up);
            result.Add(down);
        }      

        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){
            result.Add(up);
            result.Add(down);
        }      

        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();
            set.Add(current.val);
            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){
        set.Add(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();
            set.Add(current.val);
            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 {
    public ListNode DetectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

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

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

                return slow;
            }
        }

        return null;
    }
}


活动打卡代码 LeetCode 142. 环形链表 II

hpstory
9小时前
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode DetectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

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

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

                return slow;
            }
        }

        return null;
    }
}



hpstory
13小时前

C# 代码

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

        return slow;
    }
}



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

        return slow;
    }
}