Skip to content

Commit 3777781

Browse files
authored
Updated tasks 23-49
1 parent 22223a1 commit 3777781

File tree

19 files changed

+127
-156
lines changed

19 files changed

+127
-156
lines changed

src/main/java/g0001_0100/s0023_merge_k_sorted_lists/readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ _Merge all the linked-lists into one sorted linked-list and return it._
1212

1313
**Output:** [1,1,2,3,4,4,5,6]
1414

15-
**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6
15+
**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6
1616

1717
**Example 2:**
1818

@@ -29,11 +29,11 @@ _Merge all the linked-lists into one sorted linked-list and return it._
2929
**Constraints:**
3030

3131
* `k == lists.length`
32-
* `0 <= k <= 10^4`
32+
* <code>0 <= k <= 10<sup>4</sup></code>
3333
* `0 <= lists[i].length <= 500`
34-
* `-10^4 <= lists[i][j] <= 10^4`
34+
* <code>-10<sup>4</sup> <= lists[i][j] <= 10<sup>4</sup></code>
3535
* `lists[i]` is sorted in **ascending order**.
36-
* The sum of `lists[i].length` won't exceed `10^4`.
36+
* The sum of `lists[i].length` will not exceed <code>10<sup>4</sup></code>.
3737

3838
To solve the "Merge k Sorted Lists" problem in Java with a `Solution` class, we can use a priority queue (min-heap) to efficiently merge the lists. Here are the steps:
3939

@@ -130,4 +130,4 @@ class ListNode {
130130
}
131131
```
132132

133-
This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.
133+
This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.

src/main/java/g0001_0100/s0024_swap_nodes_in_pairs/readme.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
66

77
**Example 1:**
88

9-
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
10-
119
**Input:** head = [1,2,3,4]
1210

13-
**Output:** [2,1,4,3]
11+
**Output:** [2,1,4,3]
12+
13+
**Explanation:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
1416

1517
**Example 2:**
1618

1719
**Input:** head = []
1820

19-
**Output:** []
21+
**Output:** []
2022

2123
**Example 3:**
2224

2325
**Input:** head = [1]
2426

25-
**Output:** [1]
27+
**Output:** [1]
28+
29+
**Example 4:**
30+
31+
**Input:** head = [1,2,3]
32+
33+
**Output:** [2,1,3]
2634

2735
**Constraints:**
2836

@@ -76,4 +84,4 @@ public class Solution {
7684
}
7785
```
7886

79-
This implementation provides a solution to the "Swap Nodes in Pairs" problem in Java without modifying the values in the list's nodes.
87+
This implementation provides a solution to the "Swap Nodes in Pairs" problem in Java without modifying the values in the list's nodes.

src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Hard
44

5-
Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list.
5+
Given the `head` of a linked list, reverse the nodes of the list `k` at a time, and return _the modified list_.
66

7-
_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes, in the end, should remain as it is.
7+
`k` is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of `k` then left-out nodes, in the end, should remain as it is.
88

99
You may not alter the values in the list's nodes, only nodes themselves may be changed.
1010

@@ -24,26 +24,13 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
2424

2525
**Output:** [3,2,1,4,5]
2626

27-
**Example 3:**
28-
29-
**Input:** head = [1,2,3,4,5], k = 1
30-
31-
**Output:** [1,2,3,4,5]
32-
33-
**Example 4:**
34-
35-
**Input:** head = [1], k = 1
36-
37-
**Output:** [1]
38-
3927
**Constraints:**
4028

41-
* The number of nodes in the list is in the range `sz`.
42-
* `1 <= sz <= 5000`
29+
* The number of nodes in the list is `n`.
30+
* `1 <= k <= n <= 5000`
4331
* `0 <= Node.val <= 1000`
44-
* `1 <= k <= sz`
4532

46-
**Follow-up:** Can you solve the problem in O(1) extra memory space?
33+
**Follow-up:** Can you solve the problem in `O(1)` extra memory space?
4734

4835
To solve the "Reverse Nodes in k-Group" problem in Java with a `Solution` class, we can reverse the nodes in groups of k using a recursive approach. Here are the steps:
4936

@@ -109,4 +96,4 @@ public class Solution {
10996
}
11097
```
11198

112-
This implementation provides a solution to the "Reverse Nodes in k-Group" problem in Java without modifying the values in the list's nodes. It recursively reverses the nodes in groups of k.
99+
This implementation provides a solution to the "Reverse Nodes in k-Group" problem in Java without modifying the values in the list's nodes. It recursively reverses the nodes in groups of k.

src/main/java/g0001_0100/s0026_remove_duplicates_from_sorted_array/readme.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ Easy
44

55
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
66

7-
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
7+
Consider the number of _unique elements_ in `nums` to be `k`. After removing duplicates, return the number of unique elements `k`.
88

9-
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10-
11-
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
9+
The first `k` elements of `nums` should contain the unique numbers in **sorted order**. The remaining elements beyond index `k - 1` can be ignored.
1210

1311
**Custom Judge:**
1412

@@ -22,7 +20,7 @@ The judge will test your solution with the following code:
2220
assert k == expectedNums.length;
2321
for (int i = 0; i < k; i++) {
2422
assert nums[i] == expectedNums[i];
25-
}
23+
}
2624

2725
If all assertions pass, then your solution will be **accepted**.
2826

@@ -44,6 +42,6 @@ If all assertions pass, then your solution will be **accepted**.
4442

4543
**Constraints:**
4644

47-
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
45+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
4846
* `-100 <= nums[i] <= 100`
49-
* `nums` is sorted in **non-decreasing** order.
47+
* `nums` is sorted in **non-decreasing** order.

src/main/java/g0001_0100/s0027_remove_element/readme.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
Easy
44

5-
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The relative order of the elements may be changed.
5+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The order of the elements may be changed. Then return _the number of elements in_ `nums` _which are not equal to_ `val`.
66

7-
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
7+
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:
88

9-
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10-
11-
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
9+
* Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
10+
* Return `k`.
1211

1312
**Custom Judge:**
1413

@@ -25,7 +24,7 @@ The judge will test your solution with the following code:
2524
sort(nums, 0, k); // Sort the first k elements of nums
2625
for (int i = 0; i < actualLength; i++) {
2726
assert nums[i] == expectedNums[i];
28-
}
27+
}
2928

3029
If all assertions pass, then your solution will be **accepted**.
3130

@@ -49,4 +48,4 @@ If all assertions pass, then your solution will be **accepted**.
4948

5049
* `0 <= nums.length <= 100`
5150
* `0 <= nums[i] <= 50`
52-
* `0 <= val <= 100`
51+
* `0 <= val <= 100`
Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
1-
28\. Implement strStr()
1+
28\. Find the Index of the First Occurrence in a String
22

33
Easy
44

5-
Implement [strStr()](http://www.cplusplus.com/reference/cstring/strstr/).
6-
7-
Return the index of the first occurrence of needle in haystack, or `-1` if `needle` is not part of `haystack`.
8-
9-
**Clarification:**
10-
11-
What should we return when `needle` is an empty string? This is a great question to ask during an interview.
12-
13-
For the purpose of this problem, we will return 0 when `needle` is an empty string. This is consistent to C's [strstr()](http://www.cplusplus.com/reference/cstring/strstr/) and Java's [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)).
5+
Given two strings `needle` and `haystack`, return the index of the first occurrence of `needle` in `haystack`, or `-1` if `needle` is not part of `haystack`.
146

157
**Example 1:**
168

17-
**Input:** haystack = "hello", needle = "ll"
9+
**Input:** haystack = "sadbutsad", needle = "sad"
1810

19-
**Output:** 2
11+
**Output:** 0
2012

21-
**Example 2:**
22-
23-
**Input:** haystack = "aaaaa", needle = "bba"
13+
**Explanation:** "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.
2414

25-
**Output:** -1
15+
**Example 2:**
2616

27-
**Example 3:**
17+
**Input:** haystack = "leetcode", needle = "leeto"
2818

29-
**Input:** haystack = "", needle = ""
19+
**Output:** -1
3020

31-
**Output:** 0
21+
**Explanation:** "leeto" did not occur in "leetcode", so we return -1.
3222

3323
**Constraints:**
3424

35-
* <code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code>
36-
* `haystack` and `needle` consist of only lower-case English characters.
25+
* <code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code>
26+
* `haystack` and `needle` consist of only lowercase English characters.

src/main/java/g0001_0100/s0030_substring_with_concatenation_of_all_words/readme.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@
22

33
Hard
44

5-
You are given a string `s` and an array of strings `words` of **the same length**. Return all starting indices of substring(s) in `s` that is a concatenation of each word in `words` **exactly once**, **in any order**, and **without any intervening characters**.
5+
You are given a string `s` and an array of strings `words`. All the strings of `words` are of **the same length**.
66

7-
You can return the answer in **any order**.
7+
A **concatenated string** is a string that exactly contains all the strings of any permutation of `words` concatenated.
8+
9+
* For example, if `words = ["ab","cd","ef"]`, then `"abcdef"`, `"abefcd"`, `"cdabef"`, `"cdefab"`, `"efabcd"`, and `"efcdab"` are all concatenated strings. `"acdbef"` is not a concatenated string because it is not the concatenation of any permutation of `words`.
10+
11+
Return an array of _the starting indices_ of all the concatenated substrings in `s`. You can return the answer in **any order**.
812

913
**Example 1:**
1014

1115
**Input:** s = "barfoothefoobarman", words = ["foo","bar"]
1216

1317
**Output:** [0,9]
1418

15-
**Explanation:** Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.
19+
**Explanation:**
20+
21+
The substring starting at 0 is `"barfoo"`. It is the concatenation of `["bar","foo"]` which is a permutation of `words`.
22+
The substring starting at 9 is `"foobar"`. It is the concatenation of `["foo","bar"]` which is a permutation of `words`.
1623

1724
**Example 2:**
1825

1926
**Input:** s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
2027

21-
**Output:** []
28+
**Output:** []
29+
30+
**Explanation:**
31+
32+
There is no concatenated substring.
2233

2334
**Example 3:**
2435

2536
**Input:** s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
2637

27-
**Output:** [6,9,12]
38+
**Output:** [6,9,12]
39+
40+
**Explanation:**
41+
42+
The substring starting at 6 is `"foobarthe"`. It is the concatenation of `["foo","bar","the"]`.
43+
The substring starting at 9 is `"barthefoo"`. It is the concatenation of `["bar","the","foo"]`.
44+
The substring starting at 12 is `"thefoobar"`. It is the concatenation of `["the","foo","bar"]`.
2845

2946
**Constraints:**
3047

3148
* <code>1 <= s.length <= 10<sup>4</sup></code>
32-
* `s` consists of lower-case English letters.
3349
* `1 <= words.length <= 5000`
3450
* `1 <= words[i].length <= 30`
35-
* `words[i]` consists of lower-case English letters.
51+
* `s` and `words[i]` consist of lowercase English letters.

src/main/java/g0001_0100/s0031_next_permutation/readme.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
Medium
44

5-
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
5+
A **permutation** of an array of integers is an arrangement of its members into a sequence or linear order.
66

7-
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
7+
* For example, for `arr = [1,2,3]`, the following are all the permutations of `arr`: `[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]`.
8+
9+
The **next permutation** of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the **next permutation** of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
10+
11+
* For example, the next permutation of `arr = [1,2,3]` is `[1,3,2]`.
12+
* Similarly, the next permutation of `arr = [2,3,1]` is `[3,1,2]`.
13+
* While the next permutation of `arr = [3,2,1]` is `[1,2,3]` because `[3,2,1]` does not have a lexicographical larger rearrangement.
14+
15+
Given an array of integers `nums`, _find the next permutation of_ `nums`.
816

917
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
1018

@@ -26,12 +34,6 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor
2634

2735
**Output:** [1,5,1]
2836

29-
**Example 4:**
30-
31-
**Input:** nums = [1]
32-
33-
**Output:** [1]
34-
3537
**Constraints:**
3638

3739
* `1 <= nums.length <= 100`
@@ -96,4 +98,4 @@ public class Solution {
9698
}
9799
```
98100

99-
This implementation provides a solution to the "Next Permutation" problem in Java. It finds the next lexicographically greater permutation of the given array `nums` and modifies it in place.
101+
This implementation provides a solution to the "Next Permutation" problem in Java. It finds the next lexicographically greater permutation of the given array `nums` and modifies it in place.

src/main/java/g0001_0100/s0032_longest_valid_parentheses/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Hard
44

5-
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
5+
Given a string containing just the characters `'('` and `')'`, return _the length of the longest valid (well-formed) parentheses_ **substring**.
66

77
**Example 1:**
88

@@ -76,4 +76,4 @@ public class Solution {
7676
}
7777
```
7878

79-
This implementation provides a solution to the "Longest Valid Parentheses" problem in Java. It finds the length of the longest valid parentheses substring in the given string `s`.
79+
This implementation provides a solution to the "Longest Valid Parentheses" problem in Java. It finds the length of the longest valid parentheses substring in the given string `s`.

src/main/java/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Medium
44

55
There is an integer array `nums` sorted in ascending order (with **distinct** values).
66

7-
Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`.
7+
Prior to being passed to your function, `nums` is **possibly left rotated** at an unknown index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be left rotated by `3` indices and become `[4,5,6,7,0,1,2]`.
88

99
Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`.
1010

@@ -88,4 +88,4 @@ public class Solution {
8888
}
8989
```
9090

91-
This implementation provides a solution to the "Search in Rotated Sorted Array" problem in Java. It searches for the index of `target` in the rotated sorted array `nums`. The algorithm has a time complexity of O(log n).
91+
This implementation provides a solution to the "Search in Rotated Sorted Array" problem in Java. It searches for the index of `target` in the rotated sorted array `nums`. The algorithm has a time complexity of O(log n).

0 commit comments

Comments
 (0)