Skip to content

Commit 1509e95

Browse files
committed
Added tasks 61-100
1 parent f2c124c commit 1509e95

File tree

16 files changed

+1084
-20
lines changed

16 files changed

+1084
-20
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
## 61\. Rotate List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, rotate the list to the right by `k` places.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
13+
14+
**Input:** head = [1,2,3,4,5], k = 2
15+
16+
**Output:** [4,5,1,2,3]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
21+
22+
**Input:** head = [0,1,2], k = 4
23+
24+
**Output:** [2,0,1]
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the list is in the range `[0, 500]`.
29+
* `-100 <= Node.val <= 100`
30+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
31+
32+
## Solution
33+
34+
```csharp
35+
using LeetCodeNet.Com_github_leetcode;
36+
37+
/**
38+
* Definition for singly-linked list.
39+
* public class ListNode {
40+
* public int val;
41+
* public ListNode next;
42+
* public ListNode(int val=0, ListNode next=null) {
43+
* this.val = val;
44+
* this.next = next;
45+
* }
46+
* }
47+
*/
48+
public class Solution {
49+
public ListNode RotateRight(ListNode head, int k) {
50+
if (head == null || head.next == null || k == 0) {
51+
return head;
52+
}
53+
// Compute the length
54+
int len = 1;
55+
ListNode tail = head;
56+
while (tail.next != null) {
57+
tail = tail.next;
58+
len++;
59+
}
60+
k = k % len;
61+
if (k == 0) {
62+
return head;
63+
}
64+
// Make it a circle
65+
tail.next = head;
66+
// Find new tail: (len - k - 1)th node
67+
ListNode newTail = head;
68+
for (int i = 0; i < len - k - 1; i++) {
69+
newTail = newTail.next;
70+
}
71+
ListNode newHead = newTail.next;
72+
newTail.next = null;
73+
return newHead;
74+
}
75+
}
76+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
## 63\. Unique Paths II
5+
6+
Medium
7+
8+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
9+
10+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
11+
12+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
13+
14+
An obstacle and space is marked as `1` and `0` respectively in the grid.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
19+
20+
**Input:** obstacleGrid = \[\[0,0,0],[0,1,0],[0,0,0]]
21+
22+
**Output:** 2
23+
24+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
29+
30+
**Input:** obstacleGrid = \[\[0,1],[0,0]]
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* `m == obstacleGrid.length`
37+
* `n == obstacleGrid[i].length`
38+
* `1 <= m, n <= 100`
39+
* `obstacleGrid[i][j]` is `0` or `1`.
40+
41+
## Solution
42+
43+
```csharp
44+
public class Solution {
45+
public int UniquePathsWithObstacles(int[][] obstacleGrid) {
46+
int m = obstacleGrid.Length;
47+
int n = obstacleGrid[0].Length;
48+
int[,] dp = new int[m, n];
49+
for (int i = 0; i < m; i++) {
50+
for (int j = 0; j < n; j++) {
51+
if (obstacleGrid[i][j] == 1) {
52+
dp[i, j] = 0;
53+
} else if (i == 0 && j == 0) {
54+
dp[i, j] = 1;
55+
} else {
56+
dp[i, j] = (i > 0 ? dp[i - 1, j] : 0) + (j > 0 ? dp[i, j - 1] : 0);
57+
}
58+
}
59+
}
60+
return dp[m - 1, n - 1];
61+
}
62+
}
63+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
## 66\. Plus One
5+
6+
Easy
7+
8+
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the `ith` digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
9+
10+
Increment the large integer by one and return _the resulting array of digits_.
11+
12+
**Example 1:**
13+
14+
**Input:** digits = [1,2,3]
15+
16+
**Output:** [1,2,4]
17+
18+
**Explanation:** The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
19+
20+
**Example 2:**
21+
22+
**Input:** digits = [4,3,2,1]
23+
24+
**Output:** [4,3,2,2]
25+
26+
**Explanation:** The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
27+
28+
**Example 3:**
29+
30+
**Input:** digits = [0]
31+
32+
**Output:** [1]
33+
34+
**Explanation:** The array represents the integer 0. Incrementing by one gives 0 + 1 = 1. Thus, the result should be [1].
35+
36+
**Example 4:**
37+
38+
**Input:** digits = [9]
39+
40+
**Output:** [1,0]
41+
42+
**Explanation:** The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
43+
44+
**Constraints:**
45+
46+
* `1 <= digits.length <= 100`
47+
* `0 <= digits[i] <= 9`
48+
* `digits` does not contain any leading `0`'s.
49+
50+
## Solution
51+
52+
```csharp
53+
public class Solution {
54+
public int[] PlusOne(int[] digits) {
55+
for (int i = digits.Length - 1; i >= 0; i--) {
56+
if (digits[i] < 9) {
57+
digits[i]++;
58+
return digits;
59+
}
60+
digits[i] = 0;
61+
}
62+
int[] res = new int[digits.Length + 1];
63+
res[0] = 1;
64+
return res;
65+
}
66+
}
67+
```
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+
## 67\. Add Binary
5+
6+
Easy
7+
8+
Given two binary strings `a` and `b`, return _their sum as a binary string_.
9+
10+
**Example 1:**
11+
12+
**Input:** a = "11", b = "1"
13+
14+
**Output:** "100"
15+
16+
**Example 2:**
17+
18+
**Input:** a = "1010", b = "1011"
19+
20+
**Output:** "10101"
21+
22+
**Constraints:**
23+
24+
* <code>1 <= a.length, b.length <= 10<sup>4</sup></code>
25+
* `a` and `b` consist only of `'0'` or `'1'` characters.
26+
* Each string does not contain leading zeros except for the zero itself.
27+
28+
## Solution
29+
30+
```csharp
31+
public class Solution {
32+
public string AddBinary(string a, string b) {
33+
var sb = new System.Text.StringBuilder();
34+
int i = a.Length - 1;
35+
int j = b.Length - 1;
36+
int carry = 0;
37+
while (i >= 0 || j >= 0 || carry > 0) {
38+
int sum = carry;
39+
if (i >= 0) {
40+
sum += a[i--] - '0';
41+
}
42+
if (j >= 0) {
43+
sum += b[j--] - '0';
44+
}
45+
sb.Insert(0, (char)('0' + (sum % 2)));
46+
carry = sum / 2;
47+
}
48+
return sb.ToString();
49+
}
50+
}
51+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
## 68\. Text Justification
5+
6+
Hard
7+
8+
Given an array of strings `words` and a width `maxWidth`, format the text such that each line has exactly `maxWidth` characters and is fully (left and right) justified.
9+
10+
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces `' '` when necessary so that each line has exactly `maxWidth` characters.
11+
12+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
13+
14+
For the last line of text, it should be left-justified and no extra space is inserted between words.
15+
16+
**Note:**
17+
18+
* A word is defined as a character sequence consisting of non-space characters only.
19+
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
20+
* The input array `words` contains at least one word.
21+
22+
**Example 1:**
23+
24+
**Input:** words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
25+
26+
**Output:** [ "This is an", "example of text", "justification. " ]
27+
28+
**Example 2:**
29+
30+
**Input:** words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
31+
32+
**Output:** [ "What must be", "acknowledgment ", "shall be " ]
33+
34+
**Explanation:** Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word.
35+
36+
**Example 3:**
37+
38+
**Input:** words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
39+
40+
**Output:** [ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ]
41+
42+
**Constraints:**
43+
44+
* `1 <= words.length <= 300`
45+
* `1 <= words[i].length <= 20`
46+
* `words[i]` consists of only English letters and symbols.
47+
* `1 <= maxWidth <= 100`
48+
* `words[i].length <= maxWidth`
49+
50+
## Solution
51+
52+
```csharp
53+
public class Solution {
54+
public IList<string> FullJustify(string[] words, int maxWidth) {
55+
var res = new List<string>();
56+
int i = 0;
57+
int n = words.Length;
58+
while (i < n) {
59+
int j = i, lineLen = 0;
60+
while (j < n && lineLen + words[j].Length + (j - i) <= maxWidth) {
61+
lineLen += words[j].Length;
62+
j++;
63+
}
64+
int spaces = maxWidth - lineLen;
65+
int gaps = j - i - 1;
66+
var line = new System.Text.StringBuilder();
67+
if (j == n || gaps == 0) {
68+
for (int k = i; k < j; k++) {
69+
line.Append(words[k]);
70+
if (k < j - 1) {
71+
line.Append(' ');
72+
}
73+
}
74+
line.Append(' ', maxWidth - line.Length);
75+
} else {
76+
int spaceEach = spaces / gaps;
77+
int extra = spaces % gaps;
78+
for (int k = i; k < j; k++) {
79+
line.Append(words[k]);
80+
if (k < j - 1) {
81+
line.Append(' ', spaceEach + (k - i < extra ? 1 : 0));
82+
}
83+
}
84+
}
85+
res.Add(line.ToString());
86+
i = j;
87+
}
88+
return res;
89+
}
90+
}
91+
```

0 commit comments

Comments
 (0)