Skip to content

Added tasks 129-135 #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void SumNumbers() {
var treeNode = TreeNode.Create(new List<int?> {1, 2, 3});
Assert.Equal(25, new Solution().SumNumbers(treeNode));
}

[Fact]
public void SumNumbers2() {
var treeNode = TreeNode.Create(new List<int?> {4, 9, 0, 5, 1});
Assert.Equal(1026, new Solution().SumNumbers(treeNode));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions {

using Xunit;

public class SolutionTest {
[Fact]
public void Solve() {
char[][] board = new char[][] {
new char[] {'X', 'X', 'X', 'X'},
new char[] {'X', 'O', 'O', 'X'},
new char[] {'X', 'X', 'O', 'X'},
new char[] {'X', 'O', 'X', 'X'}
};
new Solution().Solve(board);
char[][] expected = new char[][] {
new char[] {'X', 'X', 'X', 'X'},
new char[] {'X', 'X', 'X', 'X'},
new char[] {'X', 'X', 'X', 'X'},
new char[] {'X', 'O', 'X', 'X'}
};
Assert.Equal(expected, board);
}

[Fact]
public void Solve2() {
char[][] board = new char[][] {new char[] {'X'}};
new Solution().Solve(board);
char[][] expected = new char[][] {new char[] {'X'}};
Assert.Equal(expected, board);
}
}
}
32 changes: 32 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0133_clone_graph/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace LeetCodeNet.G0101_0200.S0133_clone_graph {

using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void CloneGraph() {
var node1 = new Node(1);
var node2 = new Node(2);
var node3 = new Node(3);
var node4 = new Node(4);
var node1and2and4 = new Node(1, new List<Node> {node2, node4});
var node2and1and3 = new Node(2, new List<Node> {node1, node3});
var node3and2and4 = new Node(3, new List<Node> {node2, node4});
var node4and1and3 = new Node(4, new List<Node> {node1, node3});
var node = new Node(5, new List<Node> {node1and2and4, node2and1and3, node3and2and4, node4and1and3});
Assert.Equal("[[2,4],[1,3],[2,4],[1,3]]", new Solution().CloneGraph(node).ToString());
}

[Fact]
public void CloneGraph2() {
var node1 = new Node(1);
Assert.Equal("[]", new Solution().CloneGraph(node1).ToString());
}

[Fact]
public void CloneGraph3() {
Assert.Null(new Solution().CloneGraph(null));
}
}
}
16 changes: 16 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0134_gas_station/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0101_0200.S0134_gas_station {

using Xunit;

public class SolutionTest {
[Fact]
public void CanCompleteCircuit() {
Assert.Equal(3, new Solution().CanCompleteCircuit(new int[] {1, 2, 3, 4, 5}, new int[] {3, 4, 5, 1, 2}));
}

[Fact]
public void CanCompleteCircuit2() {
Assert.Equal(-1, new Solution().CanCompleteCircuit(new int[] {2, 3, 4}, new int[] {3, 4, 3}));
}
}
}
16 changes: 16 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0135_candy/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0101_0200.S0135_candy {

using Xunit;

public class SolutionTest {
[Fact]
public void Candy() {
Assert.Equal(5, new Solution().Candy(new int[] {1, 0, 2}));
}

[Fact]
public void Candy2() {
Assert.Equal(4, new Solution().Candy(new int[] {1, 2, 2}));
}
}
}
42 changes: 42 additions & 0 deletions LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers {

// #Medium #Depth_First_Search #Tree #Binary_Tree #Top_Interview_150_Binary_Tree_General
// #2025_07_12_Time_0_ms_(100.00%)_Space_41.52_MB_(70.15%)

using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int sum = 0;

public int SumNumbers(TreeNode root) {
RecurseSum(root, 0);
return sum;
}

private void RecurseSum(TreeNode node, int curNum) {
if (node.left == null && node.right == null) {
sum += 10 * curNum + node.val.Value;
} else {
if (node.left != null) {
RecurseSum(node.left, 10 * curNum + node.val.Value);
}
if (node.right != null) {
RecurseSum(node.right, 10 * curNum + node.val.Value);
}
}
}
}
}
39 changes: 39 additions & 0 deletions LeetCodeNet/G0101_0200/S0129_sum_root_to_leaf_numbers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
129\. Sum Root to Leaf Numbers

Medium

You are given the `root` of a binary tree containing digits from `0` to `9` only.

Each root-to-leaf path in the tree represents a number.

* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`.

Return _the total sum of all root-to-leaf numbers_. Test cases are generated so that the answer will fit in a **32-bit** integer.

A **leaf** node is a node with no children.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg)

**Input:** root = [1,2,3]

**Output:** 25

**Explanation:** The root-to-leaf path `1->2` represents the number `12`. The root-to-leaf path `1->3` represents the number `13`. Therefore, sum = 12 + 13 = `25`.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg)

**Input:** root = [4,9,0,5,1]

**Output:** 1026

**Explanation:** The root-to-leaf path `4->9->5` represents the number 495. The root-to-leaf path `4->9->1` represents the number 491. The root-to-leaf path `4->0` represents the number 40. Therefore, sum = 495 + 491 + 40 = `1026`.

**Constraints:**

* The number of nodes in the tree is in the range `[1, 1000]`.
* `0 <= Node.val <= 9`
* The depth of the tree will not exceed `10`.
51 changes: 51 additions & 0 deletions LeetCodeNet/G0101_0200/S0130_surrounded_regions/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions {

// #Medium #Top_Interview_Questions #Array #Depth_First_Search #Breadth_First_Search #Matrix
// #Union_Find #Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search
// #Top_Interview_150_Graph_General #2025_07_12_Time_1_ms_(100.00%)_Space_64.38_MB_(92.86%)

public class Solution {
public void Solve(char[][] board) {
if (board.Length == 0) {
return;
}
for (int i = 0; i < board[0].Length; i++) {
if (board[0][i] == 'O') {
Dfs(board, 0, i);
}
if (board[board.Length - 1][i] == 'O') {
Dfs(board, board.Length - 1, i);
}
}
for (int i = 0; i < board.Length; i++) {
if (board[i][0] == 'O') {
Dfs(board, i, 0);
}
if (board[i][board[0].Length - 1] == 'O') {
Dfs(board, i, board[0].Length - 1);
}
}
for (int i = 0; i < board.Length; i++) {
for (int j = 0; j < board[0].Length; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == '#') {
board[i][j] = 'O';
}
}
}
}

private void Dfs(char[][] board, int row, int column) {
if (row < 0 || row >= board.Length || column < 0 || column >= board[0].Length || board[row][column] != 'O') {
return;
}
board[row][column] = '#';
Dfs(board, row + 1, column);
Dfs(board, row - 1, column);
Dfs(board, row, column + 1);
Dfs(board, row, column - 1);
}
}
}
30 changes: 30 additions & 0 deletions LeetCodeNet/G0101_0200/S0130_surrounded_regions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
130\. Surrounded Regions

Medium

Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.

A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)

**Input:** board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]

**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]

**Explanation:** Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.

**Example 2:**

**Input:** board = [["X"]]

**Output:** [["X"]]

**Constraints:**

* `m == board.length`
* `n == board[i].length`
* `1 <= m, n <= 200`
* `board[i][j]` is `'X'` or `'O'`.
47 changes: 47 additions & 0 deletions LeetCodeNet/G0101_0200/S0133_clone_graph/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace LeetCodeNet.G0101_0200.S0133_clone_graph {
public class Node {
public int val;
public IList<Node> neighbors;

public Node() {
val = 0;
neighbors = new List<Node>();
}

public Node(int _val) {
val = _val;
neighbors = new List<Node>();
}

public Node(int _val, List<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}

public override string ToString() {
var result = new System.Text.StringBuilder();
result.Append("[");
bool first = true;
foreach (var node in neighbors) {
if (!first) result.Append(",");
if (node.neighbors == null || node.neighbors.Count == 0) {
result.Append(node.val);
} else {
var inner = new System.Text.StringBuilder();
inner.Append("[");
bool innerFirst = true;
foreach (var nodeItem in node.neighbors) {
if (!innerFirst) inner.Append(",");
inner.Append(nodeItem.val);
innerFirst = false;
}
inner.Append("]");
result.Append(inner.ToString());
}
first = false;
}
result.Append("]");
return result.ToString();
}
}
}
55 changes: 55 additions & 0 deletions LeetCodeNet/G0101_0200/S0133_clone_graph/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace LeetCodeNet.G0101_0200.S0133_clone_graph {

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
// #Top_Interview_150_Graph_General #2025_07_12_Time_117_ms_(96.34%)_Space_47.31_MB_(97.95%)

using System.Collections.Generic;

/*
// Definition for a Node.
public class Node {
public int val;
public IList<Node> neighbors;

public Node() {
val = 0;
neighbors = new List<Node>();
}

public Node(int _val) {
val = _val;
neighbors = new List<Node>();
}

public Node(int _val, List<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/

public class Solution {
public Node CloneGraph(Node node) {
return CloneGraph(node, new Dictionary<Node, Node>());
}

private Node CloneGraph(Node node, Dictionary<Node, Node> processedNodes) {
if (node == null) {
return null;
} else if (processedNodes.ContainsKey(node)) {
return processedNodes[node];
}
Node newNode = new Node();
processedNodes[node] = newNode;
newNode.val = node.val;
newNode.neighbors = new List<Node>();
foreach (Node neighbor in node.neighbors) {
Node clonedNeighbor = CloneGraph(neighbor, processedNodes);
if (clonedNeighbor != null) {
newNode.neighbors.Add(clonedNeighbor);
}
}
return newNode;
}
}
}
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy