Skip to content

Commit e83b5b7

Browse files
committed
Added tasks 129-162
1 parent 2c2f45d commit e83b5b7

File tree

11 files changed

+840
-0
lines changed

11 files changed

+840
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 129\. Sum Root to Leaf Numbers
5+
6+
Medium
7+
8+
You are given the `root` of a binary tree containing digits from `0` to `9` only.
9+
10+
Each root-to-leaf path in the tree represents a number.
11+
12+
* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`.
13+
14+
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.
15+
16+
A **leaf** node is a node with no children.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg)
21+
22+
**Input:** root = [1,2,3]
23+
24+
**Output:** 25
25+
26+
**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`.
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg)
31+
32+
**Input:** root = [4,9,0,5,1]
33+
34+
**Output:** 1026
35+
36+
**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`.
37+
38+
**Constraints:**
39+
40+
* The number of nodes in the tree is in the range `[1, 1000]`.
41+
* `0 <= Node.val <= 9`
42+
* The depth of the tree will not exceed `10`.
43+
44+
## Solution
45+
46+
```csharp
47+
using LeetCodeNet.Com_github_leetcode;
48+
49+
/**
50+
* Definition for a binary tree node.
51+
* public class TreeNode {
52+
* public int val;
53+
* public TreeNode left;
54+
* public TreeNode right;
55+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
56+
* this.val = val;
57+
* this.left = left;
58+
* this.right = right;
59+
* }
60+
* }
61+
*/
62+
public class Solution {
63+
private int sum = 0;
64+
65+
public int SumNumbers(TreeNode root) {
66+
RecurseSum(root, 0);
67+
return sum;
68+
}
69+
70+
private void RecurseSum(TreeNode node, int curNum) {
71+
if (node.left == null && node.right == null) {
72+
sum += 10 * curNum + node.val.Value;
73+
} else {
74+
if (node.left != null) {
75+
RecurseSum(node.left, 10 * curNum + node.val.Value);
76+
}
77+
if (node.right != null) {
78+
RecurseSum(node.right, 10 * curNum + node.val.Value);
79+
}
80+
}
81+
}
82+
}
83+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 130\. Surrounded Regions
5+
6+
Medium
7+
8+
Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.
9+
10+
A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)
15+
16+
**Input:** board = \[\["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
17+
18+
**Output:** [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** board = \[\["X"]]
25+
26+
**Output:** [["X"]]
27+
28+
**Constraints:**
29+
30+
* `m == board.length`
31+
* `n == board[i].length`
32+
* `1 <= m, n <= 200`
33+
* `board[i][j]` is `'X'` or `'O'`.
34+
35+
## Solution
36+
37+
```csharp
38+
public class Solution {
39+
public void Solve(char[][] board) {
40+
if (board.Length == 0) {
41+
return;
42+
}
43+
for (int i = 0; i < board[0].Length; i++) {
44+
if (board[0][i] == 'O') {
45+
Dfs(board, 0, i);
46+
}
47+
if (board[board.Length - 1][i] == 'O') {
48+
Dfs(board, board.Length - 1, i);
49+
}
50+
}
51+
for (int i = 0; i < board.Length; i++) {
52+
if (board[i][0] == 'O') {
53+
Dfs(board, i, 0);
54+
}
55+
if (board[i][board[0].Length - 1] == 'O') {
56+
Dfs(board, i, board[0].Length - 1);
57+
}
58+
}
59+
for (int i = 0; i < board.Length; i++) {
60+
for (int j = 0; j < board[0].Length; j++) {
61+
if (board[i][j] == 'O') {
62+
board[i][j] = 'X';
63+
}
64+
if (board[i][j] == '#') {
65+
board[i][j] = 'O';
66+
}
67+
}
68+
}
69+
}
70+
71+
private void Dfs(char[][] board, int row, int column) {
72+
if (row < 0 || row >= board.Length || column < 0 || column >= board[0].Length || board[row][column] != 'O') {
73+
return;
74+
}
75+
board[row][column] = '#';
76+
Dfs(board, row + 1, column);
77+
Dfs(board, row - 1, column);
78+
Dfs(board, row, column + 1);
79+
Dfs(board, row, column - 1);
80+
}
81+
}
82+
```
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 133\. Clone Graph
5+
6+
Medium
7+
8+
Given a reference of a node in a **[connected](https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph)** undirected graph.
9+
10+
Return a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) (clone) of the graph.
11+
12+
Each node in the graph contains a value (`int`) and a list (`List[Node]`) of its neighbors.
13+
14+
class Node { public int val; public List<Node> neighbors; }
15+
16+
**Test case format:**
17+
18+
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with `val == 1`, the second node with `val == 2`, and so on. The graph is represented in the test case using an adjacency list.
19+
20+
**An adjacency list** is a collection of unordered **lists** used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
21+
22+
The given node will always be the first node with `val = 1`. You must return the **copy of the given node** as a reference to the cloned graph.
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2019/11/04/133_clone_graph_question.png)
27+
28+
**Input:** adjList = \[\[2,4],[1,3],[2,4],[1,3]]
29+
30+
**Output:** [[2,4],[1,3],[2,4],[1,3]]
31+
32+
**Explanation:**
33+
34+
There are 4 nodes in the graph.
35+
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
36+
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
37+
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
38+
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
39+
40+
**Example 2:**
41+
42+
![](https://assets.leetcode.com/uploads/2020/01/07/graph.png)
43+
44+
**Input:** adjList = \[\[]]
45+
46+
**Output:** [[]]
47+
48+
**Explanation:** Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
49+
50+
**Example 3:**
51+
52+
**Input:** adjList = []
53+
54+
**Output:** []
55+
56+
**Explanation:** This an empty graph, it does not have any nodes.
57+
58+
**Example 4:**
59+
60+
![](https://assets.leetcode.com/uploads/2020/01/07/graph-1.png)
61+
62+
**Input:** adjList = \[\[2],[1]]
63+
64+
**Output:** [[2],[1]]
65+
66+
**Constraints:**
67+
68+
* The number of nodes in the graph is in the range `[0, 100]`.
69+
* `1 <= Node.val <= 100`
70+
* `Node.val` is unique for each node.
71+
* There are no repeated edges and no self-loops in the graph.
72+
* The Graph is connected and all nodes can be visited starting from the given node.
73+
74+
## Solution
75+
76+
```csharp
77+
public class Node {
78+
public int val;
79+
public IList<Node> neighbors;
80+
81+
public Node() {
82+
val = 0;
83+
neighbors = new List<Node>();
84+
}
85+
86+
public Node(int _val) {
87+
val = _val;
88+
neighbors = new List<Node>();
89+
}
90+
91+
public Node(int _val, List<Node> _neighbors) {
92+
val = _val;
93+
neighbors = _neighbors;
94+
}
95+
96+
public override string ToString() {
97+
var result = new System.Text.StringBuilder();
98+
result.Append("[");
99+
bool first = true;
100+
foreach (var node in neighbors) {
101+
if (!first) result.Append(",");
102+
if (node.neighbors == null || node.neighbors.Count == 0) {
103+
result.Append(node.val);
104+
} else {
105+
var inner = new System.Text.StringBuilder();
106+
inner.Append("[");
107+
bool innerFirst = true;
108+
foreach (var nodeItem in node.neighbors) {
109+
if (!innerFirst) inner.Append(",");
110+
inner.Append(nodeItem.val);
111+
innerFirst = false;
112+
}
113+
inner.Append("]");
114+
result.Append(inner.ToString());
115+
}
116+
first = false;
117+
}
118+
result.Append("]");
119+
return result.ToString();
120+
}
121+
}
122+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 134\. Gas Station
5+
6+
Medium
7+
8+
There are `n` gas stations along a circular route, where the amount of gas at the <code>i<sup>th</sup></code> station is `gas[i]`.
9+
10+
You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the <code>i<sup>th</sup></code> station to its next <code>(i + 1)<sup>th</sup></code> station. You begin the journey with an empty tank at one of the gas stations.
11+
12+
Given two integer arrays `gas` and `cost`, return _the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return_ `-1`. If there exists a solution, it is **guaranteed** to be **unique**
13+
14+
**Example 1:**
15+
16+
**Input:** gas = [1,2,3,4,5], cost = [3,4,5,1,2]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
23+
Travel to station 4. Your tank = 4 - 1 + 5 = 8
24+
Travel to station 0. Your tank = 8 - 2 + 1 = 7
25+
Travel to station 1. Your tank = 7 - 3 + 2 = 6
26+
Travel to station 2. Your tank = 6 - 4 + 3 = 5
27+
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
28+
Therefore, return 3 as the starting index.
29+
30+
**Example 2:**
31+
32+
**Input:** gas = [2,3,4], cost = [3,4,3]
33+
34+
**Output:** -1
35+
36+
**Explanation:**
37+
38+
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
39+
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
40+
Travel to station 0. Your tank = 4 - 3 + 2 = 3
41+
Travel to station 1. Your tank = 3 - 3 + 3 = 3
42+
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
43+
Therefore, you can't travel around the circuit once no matter where you start.
44+
45+
**Constraints:**
46+
47+
* `gas.length == n`
48+
* `cost.length == n`
49+
* <code>1 <= n <= 10<sup>5</sup></code>
50+
* <code>0 <= gas[i], cost[i] <= 10<sup>4</sup></code>
51+
52+
## Solution
53+
54+
```csharp
55+
public class Solution {
56+
public int CanCompleteCircuit(int[] gas, int[] cost) {
57+
int index = 0;
58+
int total = 0;
59+
int current = 0;
60+
for (int i = 0; i < gas.Length; i++) {
61+
int balance = gas[i] - cost[i];
62+
total += balance;
63+
current += balance;
64+
if (current < 0) {
65+
index = i + 1;
66+
current = 0;
67+
}
68+
}
69+
return total >= 0 ? index : -1;
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)