Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/g0001_0100/s0023_merge_k_sorted_lists/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _Merge all the linked-lists into one sorted linked-list and return it._

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

**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
**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

**Example 2:**

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

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

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:

Expand Down Expand Up @@ -130,4 +130,4 @@ class ListNode {
}
```

This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.
This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.
20 changes: 14 additions & 6 deletions src/main/java/g0001_0100/s0024_swap_nodes_in_pairs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ Given a linked list, swap every two adjacent nodes and return its head. You must

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)

**Input:** head = [1,2,3,4]

**Output:** [2,1,4,3]
**Output:** [2,1,4,3]

**Explanation:**

![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)

**Example 2:**

**Input:** head = []

**Output:** []
**Output:** []

**Example 3:**

**Input:** head = [1]

**Output:** [1]
**Output:** [1]

**Example 4:**

**Input:** head = [1,2,3]

**Output:** [2,1,3]

**Constraints:**

Expand Down Expand Up @@ -76,4 +84,4 @@ public class Solution {
}
```

This implementation provides a solution to the "Swap Nodes in Pairs" problem in Java without modifying the values in the list's nodes.
This implementation provides a solution to the "Swap Nodes in Pairs" problem in Java without modifying the values in the list's nodes.
25 changes: 6 additions & 19 deletions src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Hard

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

_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.
`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.

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

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

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

**Example 3:**

**Input:** head = [1,2,3,4,5], k = 1

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

**Example 4:**

**Input:** head = [1], k = 1

**Output:** [1]

**Constraints:**

* The number of nodes in the list is in the range `sz`.
* `1 <= sz <= 5000`
* The number of nodes in the list is `n`.
* `1 <= k <= n <= 5000`
* `0 <= Node.val <= 1000`
* `1 <= k <= sz`

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

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:

Expand Down Expand Up @@ -109,4 +96,4 @@ public class Solution {
}
```

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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ Easy

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**.

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.
Consider the number of _unique elements_ in `nums` to be `k`. After removing duplicates, return the number of unique elements `k`.

Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.

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.
The first `k` elements of `nums` should contain the unique numbers in **sorted order**. The remaining elements beyond index `k - 1` can be ignored.

**Custom Judge:**

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

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

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

**Constraints:**

* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
* `-100 <= nums[i] <= 100`
* `nums` is sorted in **non-decreasing** order.
* `nums` is sorted in **non-decreasing** order.
13 changes: 6 additions & 7 deletions src/main/java/g0001_0100/s0027_remove_element/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

Easy

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.
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`.

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.
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:

Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.

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.
* 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`.
* Return `k`.

**Custom Judge:**

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

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

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

* `0 <= nums.length <= 100`
* `0 <= nums[i] <= 50`
* `0 <= val <= 100`
* `0 <= val <= 100`
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
28\. Implement strStr()
28\. Find the Index of the First Occurrence in a String

Easy

Implement [strStr()](http://www.cplusplus.com/reference/cstring/strstr/).

Return the index of the first occurrence of needle in haystack, or `-1` if `needle` is not part of `haystack`.

**Clarification:**

What should we return when `needle` is an empty string? This is a great question to ask during an interview.

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)).
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`.

**Example 1:**

**Input:** haystack = "hello", needle = "ll"
**Input:** haystack = "sadbutsad", needle = "sad"

**Output:** 2
**Output:** 0

**Example 2:**

**Input:** haystack = "aaaaa", needle = "bba"
**Explanation:** "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.

**Output:** -1
**Example 2:**

**Example 3:**
**Input:** haystack = "leetcode", needle = "leeto"

**Input:** haystack = "", needle = ""
**Output:** -1

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

**Constraints:**

* <code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code>
* `haystack` and `needle` consist of only lower-case English characters.
* <code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code>
* `haystack` and `needle` consist of only lowercase English characters.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@

Hard

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**.
You are given a string `s` and an array of strings `words`. All the strings of `words` are of **the same length**.

You can return the answer in **any order**.
A **concatenated string** is a string that exactly contains all the strings of any permutation of `words` concatenated.

* 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`.

Return an array of _the starting indices_ of all the concatenated substrings in `s`. You can return the answer in **any order**.

**Example 1:**

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

**Output:** [0,9]

**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.
**Explanation:**

The substring starting at 0 is `"barfoo"`. It is the concatenation of `["bar","foo"]` which is a permutation of `words`.
The substring starting at 9 is `"foobar"`. It is the concatenation of `["foo","bar"]` which is a permutation of `words`.

**Example 2:**

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

**Output:** []
**Output:** []

**Explanation:**

There is no concatenated substring.

**Example 3:**

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

**Output:** [6,9,12]
**Output:** [6,9,12]

**Explanation:**

The substring starting at 6 is `"foobarthe"`. It is the concatenation of `["foo","bar","the"]`.
The substring starting at 9 is `"barthefoo"`. It is the concatenation of `["bar","the","foo"]`.
The substring starting at 12 is `"thefoobar"`. It is the concatenation of `["the","foo","bar"]`.

**Constraints:**

* <code>1 <= s.length <= 10<sup>4</sup></code>
* `s` consists of lower-case English letters.
* `1 <= words.length <= 5000`
* `1 <= words[i].length <= 30`
* `words[i]` consists of lower-case English letters.
* `s` and `words[i]` consist of lowercase English letters.
20 changes: 11 additions & 9 deletions src/main/java/g0001_0100/s0031_next_permutation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

Medium

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

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
* 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]`.

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).

* For example, the next permutation of `arr = [1,2,3]` is `[1,3,2]`.
* Similarly, the next permutation of `arr = [2,3,1]` is `[3,1,2]`.
* While the next permutation of `arr = [3,2,1]` is `[1,2,3]` because `[3,2,1]` does not have a lexicographical larger rearrangement.

Given an array of integers `nums`, _find the next permutation of_ `nums`.

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

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

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

**Example 4:**

**Input:** nums = [1]

**Output:** [1]

**Constraints:**

* `1 <= nums.length <= 100`
Expand Down Expand Up @@ -96,4 +98,4 @@ public class Solution {
}
```

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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Hard

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

**Example 1:**

Expand Down Expand Up @@ -76,4 +76,4 @@ public class Solution {
}
```

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`.
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`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Medium

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

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]`.
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]`.

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`.

Expand Down Expand Up @@ -88,4 +88,4 @@ public class Solution {
}
```

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).
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).
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ public class Solution {
}
```

This implementation provides a solution to the "Find First and Last Position of Element in Sorted Array" problem in Java. It returns the starting and ending positions of `target` in `nums` using binary search, with a time complexity of O(log n).
This implementation provides a solution to the "Find First and Last Position of Element in Sorted Array" problem in Java. It returns the starting and ending positions of `target` in `nums` using binary search, with a time complexity of O(log n).
Loading