Skip to content

Commit 54f2d27

Browse files
authored
Added tasks 129-135
1 parent 5cf6c7a commit 54f2d27

File tree

16 files changed

+647
-0
lines changed

16 files changed

+647
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
using LeetCodeNet.Com_github_leetcode;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void SumNumbers() {
10+
var treeNode = TreeNode.Create(new List<int?> {1, 2, 3});
11+
Assert.Equal(25, new Solution().SumNumbers(treeNode));
12+
}
13+
14+
[Fact]
15+
public void SumNumbers2() {
16+
var treeNode = TreeNode.Create(new List<int?> {4, 9, 0, 5, 1});
17+
Assert.Equal(1026, new Solution().SumNumbers(treeNode));
18+
}
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Solve() {
8+
char[][] board = new char[][] {
9+
new char[] {'X', 'X', 'X', 'X'},
10+
new char[] {'X', 'O', 'O', 'X'},
11+
new char[] {'X', 'X', 'O', 'X'},
12+
new char[] {'X', 'O', 'X', 'X'}
13+
};
14+
new Solution().Solve(board);
15+
char[][] expected = new char[][] {
16+
new char[] {'X', 'X', 'X', 'X'},
17+
new char[] {'X', 'X', 'X', 'X'},
18+
new char[] {'X', 'X', 'X', 'X'},
19+
new char[] {'X', 'O', 'X', 'X'}
20+
};
21+
Assert.Equal(expected, board);
22+
}
23+
24+
[Fact]
25+
public void Solve2() {
26+
char[][] board = new char[][] {new char[] {'X'}};
27+
new Solution().Solve(board);
28+
char[][] expected = new char[][] {new char[] {'X'}};
29+
Assert.Equal(expected, board);
30+
}
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0101_0200.S0133_clone_graph {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void CloneGraph() {
9+
var node1 = new Node(1);
10+
var node2 = new Node(2);
11+
var node3 = new Node(3);
12+
var node4 = new Node(4);
13+
var node1and2and4 = new Node(1, new List<Node> {node2, node4});
14+
var node2and1and3 = new Node(2, new List<Node> {node1, node3});
15+
var node3and2and4 = new Node(3, new List<Node> {node2, node4});
16+
var node4and1and3 = new Node(4, new List<Node> {node1, node3});
17+
var node = new Node(5, new List<Node> {node1and2and4, node2and1and3, node3and2and4, node4and1and3});
18+
Assert.Equal("[[2,4],[1,3],[2,4],[1,3]]", new Solution().CloneGraph(node).ToString());
19+
}
20+
21+
[Fact]
22+
public void CloneGraph2() {
23+
var node1 = new Node(1);
24+
Assert.Equal("[]", new Solution().CloneGraph(node1).ToString());
25+
}
26+
27+
[Fact]
28+
public void CloneGraph3() {
29+
Assert.Null(new Solution().CloneGraph(null));
30+
}
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0134_gas_station {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void CanCompleteCircuit() {
8+
Assert.Equal(3, new Solution().CanCompleteCircuit(new int[] {1, 2, 3, 4, 5}, new int[] {3, 4, 5, 1, 2}));
9+
}
10+
11+
[Fact]
12+
public void CanCompleteCircuit2() {
13+
Assert.Equal(-1, new Solution().CanCompleteCircuit(new int[] {2, 3, 4}, new int[] {3, 4, 3}));
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0135_candy {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Candy() {
8+
Assert.Equal(5, new Solution().Candy(new int[] {1, 0, 2}));
9+
}
10+
11+
[Fact]
12+
public void Candy2() {
13+
Assert.Equal(4, new Solution().Candy(new int[] {1, 2, 2}));
14+
}
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace LeetCodeNet.G0101_0200.S0129_sum_root_to_leaf_numbers {
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Top_Interview_150_Binary_Tree_General
4+
// #2025_07_12_Time_0_ms_(100.00%)_Space_41.52_MB_(70.15%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* public int val;
12+
* public TreeNode left;
13+
* public TreeNode right;
14+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
public class Solution {
22+
private int sum = 0;
23+
24+
public int SumNumbers(TreeNode root) {
25+
RecurseSum(root, 0);
26+
return sum;
27+
}
28+
29+
private void RecurseSum(TreeNode node, int curNum) {
30+
if (node.left == null && node.right == null) {
31+
sum += 10 * curNum + node.val.Value;
32+
} else {
33+
if (node.left != null) {
34+
RecurseSum(node.left, 10 * curNum + node.val.Value);
35+
}
36+
if (node.right != null) {
37+
RecurseSum(node.right, 10 * curNum + node.val.Value);
38+
}
39+
}
40+
}
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
129\. Sum Root to Leaf Numbers
2+
3+
Medium
4+
5+
You are given the `root` of a binary tree containing digits from `0` to `9` only.
6+
7+
Each root-to-leaf path in the tree represents a number.
8+
9+
* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`.
10+
11+
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.
12+
13+
A **leaf** node is a node with no children.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg)
18+
19+
**Input:** root = [1,2,3]
20+
21+
**Output:** 25
22+
23+
**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`.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg)
28+
29+
**Input:** root = [4,9,0,5,1]
30+
31+
**Output:** 1026
32+
33+
**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`.
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range `[1, 1000]`.
38+
* `0 <= Node.val <= 9`
39+
* The depth of the tree will not exceed `10`.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace LeetCodeNet.G0101_0200.S0130_surrounded_regions {
2+
3+
// #Medium #Top_Interview_Questions #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #Union_Find #Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search
5+
// #Top_Interview_150_Graph_General #2025_07_12_Time_1_ms_(100.00%)_Space_64.38_MB_(92.86%)
6+
7+
public class Solution {
8+
public void Solve(char[][] board) {
9+
if (board.Length == 0) {
10+
return;
11+
}
12+
for (int i = 0; i < board[0].Length; i++) {
13+
if (board[0][i] == 'O') {
14+
Dfs(board, 0, i);
15+
}
16+
if (board[board.Length - 1][i] == 'O') {
17+
Dfs(board, board.Length - 1, i);
18+
}
19+
}
20+
for (int i = 0; i < board.Length; i++) {
21+
if (board[i][0] == 'O') {
22+
Dfs(board, i, 0);
23+
}
24+
if (board[i][board[0].Length - 1] == 'O') {
25+
Dfs(board, i, board[0].Length - 1);
26+
}
27+
}
28+
for (int i = 0; i < board.Length; i++) {
29+
for (int j = 0; j < board[0].Length; j++) {
30+
if (board[i][j] == 'O') {
31+
board[i][j] = 'X';
32+
}
33+
if (board[i][j] == '#') {
34+
board[i][j] = 'O';
35+
}
36+
}
37+
}
38+
}
39+
40+
private void Dfs(char[][] board, int row, int column) {
41+
if (row < 0 || row >= board.Length || column < 0 || column >= board[0].Length || board[row][column] != 'O') {
42+
return;
43+
}
44+
board[row][column] = '#';
45+
Dfs(board, row + 1, column);
46+
Dfs(board, row - 1, column);
47+
Dfs(board, row, column + 1);
48+
Dfs(board, row, column - 1);
49+
}
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
130\. Surrounded Regions
2+
3+
Medium
4+
5+
Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.
6+
7+
A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)
12+
13+
**Input:** board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
14+
15+
**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
**Input:** board = [["X"]]
22+
23+
**Output:** [["X"]]
24+
25+
**Constraints:**
26+
27+
* `m == board.length`
28+
* `n == board[i].length`
29+
* `1 <= m, n <= 200`
30+
* `board[i][j]` is `'X'` or `'O'`.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace LeetCodeNet.G0101_0200.S0133_clone_graph {
2+
public class Node {
3+
public int val;
4+
public IList<Node> neighbors;
5+
6+
public Node() {
7+
val = 0;
8+
neighbors = new List<Node>();
9+
}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
neighbors = new List<Node>();
14+
}
15+
16+
public Node(int _val, List<Node> _neighbors) {
17+
val = _val;
18+
neighbors = _neighbors;
19+
}
20+
21+
public override string ToString() {
22+
var result = new System.Text.StringBuilder();
23+
result.Append("[");
24+
bool first = true;
25+
foreach (var node in neighbors) {
26+
if (!first) result.Append(",");
27+
if (node.neighbors == null || node.neighbors.Count == 0) {
28+
result.Append(node.val);
29+
} else {
30+
var inner = new System.Text.StringBuilder();
31+
inner.Append("[");
32+
bool innerFirst = true;
33+
foreach (var nodeItem in node.neighbors) {
34+
if (!innerFirst) inner.Append(",");
35+
inner.Append(nodeItem.val);
36+
innerFirst = false;
37+
}
38+
inner.Append("]");
39+
result.Append(inner.ToString());
40+
}
41+
first = false;
42+
}
43+
result.Append("]");
44+
return result.ToString();
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)