Skip to content

Commit 1dea4ef

Browse files
authored
Added tasks 48-1143
1 parent 64a3743 commit 1dea4ef

File tree

42 files changed

+2887
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2887
-66
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
## 48\. Rotate Image
5+
6+
Medium
7+
8+
You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
9+
10+
You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotation.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg)
15+
16+
**Input:** matrix = \[\[1,2,3],[4,5,6],[7,8,9]]
17+
18+
**Output:** [[7,4,1],[8,5,2],[9,6,3]]
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg)
23+
24+
**Input:** matrix = \[\[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
25+
26+
**Output:** [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
27+
28+
**Example 3:**
29+
30+
**Input:** matrix = \[\[1]]
31+
32+
**Output:** [[1]]
33+
34+
**Example 4:**
35+
36+
**Input:** matrix = \[\[1,2],[3,4]]
37+
38+
**Output:** [[3,1],[4,2]]
39+
40+
**Constraints:**
41+
42+
* `matrix.length == n`
43+
* `matrix[i].length == n`
44+
* `1 <= n <= 20`
45+
* `-1000 <= matrix[i][j] <= 1000`
46+
47+
## Solution
48+
49+
```csharp
50+
public class Solution {
51+
public void Rotate(int[][] matrix) {
52+
(int height, int width) = (matrix.Length, matrix[0].Length);
53+
for (int row = 0; row < height - 1; row++) {
54+
for (int col = row + 1; col < width; col++) {
55+
(matrix[col][row], matrix[row][col]) = (matrix[row][col], matrix[col][row]);
56+
}
57+
}
58+
for (int col = 0; col < width / 2; col++) {
59+
int oppositeCol = width - 1 - col;
60+
for (int row = 0; row < height; row++) {
61+
(matrix[row][col], matrix[row][oppositeCol]) = (matrix[row][oppositeCol], matrix[row][col]);
62+
}
63+
}
64+
}
65+
}
66+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
## 49\. Group Anagrams
5+
6+
Medium
7+
8+
Given an array of strings `strs`, group **the anagrams** together. You can return the answer in **any order**.
9+
10+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
11+
12+
**Example 1:**
13+
14+
**Input:** strs = ["eat","tea","tan","ate","nat","bat"]
15+
16+
**Output:** [["bat"],["nat","tan"],["ate","eat","tea"]]
17+
18+
**Example 2:**
19+
20+
**Input:** strs = [""]
21+
22+
**Output:** [[""]]
23+
24+
**Example 3:**
25+
26+
**Input:** strs = ["a"]
27+
28+
**Output:** [["a"]]
29+
30+
**Constraints:**
31+
32+
* <code>1 <= strs.length <= 10<sup>4</sup></code>
33+
* `0 <= strs[i].length <= 100`
34+
* `strs[i]` consists of lowercase English letters.
35+
36+
## Solution
37+
38+
```csharp
39+
public class Solution {
40+
public IList<IList<string>> GroupAnagrams(string[] strs) {
41+
var map = new Dictionary<string, IList<string>>();
42+
// allocate memory only once and reuse it to sort the chars of each s in strs.
43+
var buffer = new char[10000];
44+
var bufferSpan = new Span<char>(buffer);
45+
foreach (var s in strs) {
46+
s.CopyTo(bufferSpan);
47+
Array.Sort(buffer, 0, s.Length);
48+
var key = new string(buffer, 0, s.Length);
49+
if (!map.TryGetValue(key, out var value)) {
50+
map[key] = value = new List<string>();
51+
}
52+
value.Add(s);
53+
}
54+
return map.Values.Cast<IList<string>>().ToList();
55+
}
56+
}
57+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
## 51\. N-Queens
5+
6+
Hard
7+
8+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
9+
10+
Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.
11+
12+
Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space, respectively.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
17+
18+
**Input:** n = 4
19+
20+
**Output:** [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
21+
22+
**Explanation:** There exist two distinct solutions to the 4-queens puzzle as shown above
23+
24+
**Example 2:**
25+
26+
**Input:** n = 1
27+
28+
**Output:** [["Q"]]
29+
30+
**Constraints:**
31+
32+
* `1 <= n <= 9`
33+
34+
## Solution
35+
36+
```csharp
37+
public class Solution {
38+
public IList<IList<string>> SolveNQueens(int n) {
39+
return this.Backtrack(n, 0, (x, y) => false).ToList();
40+
}
41+
42+
private IEnumerable<IList<string>> Backtrack(int boardSize, int y, Func<int, int, bool> check) {
43+
for (int x = 0; x < boardSize; x++) {
44+
if (check(x, y)) {
45+
continue;
46+
}
47+
string currentLine = $"{new string('.', x)}Q{new string('.', boardSize - x - 1)}";
48+
if (y == boardSize - 1) {
49+
yield return new List<string> { currentLine };
50+
continue;
51+
}
52+
var results = this.Backtrack(boardSize, y + 1, (xx, yy) =>
53+
check(xx, yy) ||
54+
(xx == x) ||
55+
(yy == (y - x) + xx) ||
56+
(yy == (x + y) - xx)
57+
);
58+
foreach (var result in results) {
59+
result.Add(currentLine);
60+
yield return result;
61+
}
62+
}
63+
}
64+
}
65+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
## 53\. Maximum Subarray
5+
6+
Easy
7+
8+
Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return _its sum_.
9+
10+
A **subarray** is a **contiguous** part of an array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [-2,1,-3,4,-1,2,1,-5,4]
15+
16+
**Output:** 6
17+
18+
**Explanation:** [4,-1,2,1] has the largest sum = 6.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [1]
23+
24+
**Output:** 1
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [5,4,-1,7,8]
29+
30+
**Output:** 23
31+
32+
**Constraints:**
33+
34+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
35+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
36+
37+
**Follow up:** If you have figured out the `O(n)` solution, try coding another solution using the **divide and conquer** approach, which is more subtle.
38+
39+
## Solution
40+
41+
```csharp
42+
public class Solution {
43+
public int MaxSubArray(int[] nums) {
44+
int maxSum = nums[0];
45+
int curSum = 0;
46+
for (int i = 0; i < nums.Length; i++) {
47+
curSum += nums[i];
48+
maxSum = Math.Max(curSum, maxSum);
49+
if (curSum < 0) {
50+
curSum = 0;
51+
}
52+
}
53+
return maxSum;
54+
}
55+
}
56+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
## 55\. Jump Game
5+
6+
Medium
7+
8+
You are given an integer array `nums`. You are initially positioned at the array's **first index**, and each element in the array represents your maximum jump length at that position.
9+
10+
Return `true` _if you can reach the last index, or_ `false` _otherwise_.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [2,3,1,1,4]
15+
16+
**Output:** true
17+
18+
**Explanation:** Jump 1 step from index 0 to 1, then 3 steps to the last index.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,2,1,0,4]
23+
24+
**Output:** false
25+
26+
**Explanation:** You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
27+
28+
**Constraints:**
29+
30+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
31+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
32+
33+
## Solution
34+
35+
```csharp
36+
public class Solution {
37+
public bool CanJump(int[] nums) {
38+
var accumulatedJumps = 0;
39+
for (var i = 0; i < nums.Length; i++) {
40+
if (i > accumulatedJumps) {
41+
return false;
42+
}
43+
if (accumulatedJumps >= nums.Length - 1) {
44+
return true;
45+
}
46+
accumulatedJumps = Math.Max(accumulatedJumps, i + nums[i]);
47+
}
48+
return false;
49+
}
50+
}
51+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
## 56\. Merge Intervals
5+
6+
Medium
7+
8+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
9+
10+
**Example 1:**
11+
12+
**Input:** intervals = \[\[1,3],[2,6],[8,10],[15,18]]
13+
14+
**Output:** [[1,6],[8,10],[15,18]]
15+
16+
**Explanation:** Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
17+
18+
**Example 2:**
19+
20+
**Input:** intervals = \[\[1,4],[4,5]]
21+
22+
**Output:** [[1,5]]
23+
24+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
25+
26+
**Constraints:**
27+
28+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
29+
* `intervals[i].length == 2`
30+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
31+
32+
## Solution
33+
34+
```csharp
35+
public class Solution {
36+
public int[][] Merge(int[][] intervals) {
37+
Array.Sort(intervals, (a, b) => a[0] - b[0]);
38+
for (int i = 1; i < intervals.Length; i++) {
39+
if (intervals[i][0] <= intervals[i - 1][1]) {
40+
intervals[i][0] = intervals[i - 1][0];
41+
intervals[i][1] = Math.Max(intervals[i - 1][1], intervals[i][1]);
42+
intervals[i - 1][0] = -1;
43+
}
44+
}
45+
return intervals.Where(obj => obj[0] != -1).ToArray();
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)