Skip to content

Commit 321374b

Browse files
authored
Added tasks 20, 21, 22, 23, 24.
1 parent 5f4bcae commit 321374b

File tree

15 files changed

+500
-0
lines changed

15 files changed

+500
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0001_0100.s0020_valid_parentheses
2+
3+
import java.util.Stack
4+
5+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
6+
// #2022_02_17_Time_3_ms_(53.65%)_Space_42.1_MB_(17.58%)
7+
8+
class Solution {
9+
fun isValid(s: String): Boolean {
10+
val stack = Stack<Char>()
11+
for (element in s) {
12+
val c = element
13+
if (c == '(' || c == '[' || c == '{') {
14+
stack.push(c)
15+
} else if (c == ')' && !stack.isEmpty() && stack.peek() == '(') {
16+
stack.pop()
17+
} else if (c == '}' && !stack.isEmpty() && stack.peek() == '{') {
18+
stack.pop()
19+
} else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') {
20+
stack.pop()
21+
} else {
22+
return false
23+
}
24+
}
25+
return stack.isEmpty()
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
20\. Valid Parentheses
2+
3+
Easy
4+
5+
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
6+
7+
An input string is valid if:
8+
9+
1. Open brackets must be closed by the same type of brackets.
10+
2. Open brackets must be closed in the correct order.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "()"
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
**Input:** s = "()[]{}"
21+
22+
**Output:** true
23+
24+
**Example 3:**
25+
26+
**Input:** s = "(]"
27+
28+
**Output:** false
29+
30+
**Example 4:**
31+
32+
**Input:** s = "([)]"
33+
34+
**Output:** false
35+
36+
**Example 5:**
37+
38+
**Input:** s = "{[]}"
39+
40+
**Output:** true
41+
42+
**Constraints:**
43+
44+
* <code>1 <= s.length <= 10<sup>4</sup></code>
45+
* `s` consists of parentheses only `'()[]{}'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0001_0100.s0021_merge_two_sorted_lists
2+
3+
import com_github_leetcode.ListNode
4+
5+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
6+
// #2022_02_17_Time_1_ms_(61.88%)_Space_42.8_MB_(22.76%)
7+
8+
class Solution {
9+
fun mergeTwoLists(l1: ListNode?, l2: ListNode?): ListNode? {
10+
var l1 = l1
11+
var l2 = l2
12+
var list: ListNode? = ListNode(-1)
13+
val head = list
14+
while (l1 != null || l2 != null) {
15+
if (l1 != null && l2 != null) {
16+
if (l1.`val` <= l2.`val`) {
17+
list!!.next = ListNode(l1.`val`)
18+
l1 = l1.next
19+
} else {
20+
list!!.next = ListNode(l2.`val`)
21+
l2 = l2.next
22+
}
23+
} else if (l1 != null) {
24+
list!!.next = ListNode(l1.`val`)
25+
l1 = l1.next
26+
} else {
27+
list!!.next = ListNode(l2!!.`val`)
28+
l2 = l2.next
29+
}
30+
list = list.next
31+
}
32+
return head!!.next
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
21\. Merge Two Sorted Lists
2+
3+
Easy
4+
5+
Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
10+
11+
**Input:** l1 = [1,2,4], l2 = [1,3,4]
12+
13+
**Output:** [1,1,2,3,4,4]
14+
15+
**Example 2:**
16+
17+
**Input:** l1 = [], l2 = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** l1 = [], l2 = [0]
24+
25+
**Output:** [0]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in both lists is in the range `[0, 50]`.
30+
* `-100 <= Node.val <= 100`
31+
* Both `l1` and `l2` are sorted in **non-decreasing** order.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0001_0100.s0022_generate_parentheses
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Backtracking #2022_02_17_Time_1_ms_(87.91%)_Space_43.6_MB_(19.97%)
5+
6+
class Solution {
7+
fun generateParenthesis(n: Int): List<String> {
8+
val sb = StringBuilder()
9+
val ans: MutableList<String> = ArrayList()
10+
return generate(sb, ans, n, n)
11+
}
12+
13+
private fun generate(sb: StringBuilder, str: MutableList<String>, open: Int, close: Int): List<String> {
14+
if (open == 0 && close == 0) {
15+
str.add(sb.toString())
16+
return str
17+
}
18+
if (open > 0) {
19+
sb.append('(')
20+
generate(sb, str, open - 1, close)
21+
sb.deleteCharAt(sb.length - 1)
22+
}
23+
if (close > 0 && open < close) {
24+
sb.append(')')
25+
generate(sb, str, open, close - 1)
26+
sb.deleteCharAt(sb.length - 1)
27+
}
28+
return str
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
22\. Generate Parentheses
2+
3+
Medium
4+
5+
Given `n` pairs of parentheses, write a function to _generate all combinations of well-formed parentheses_.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 3
10+
11+
**Output:** ["((()))","(()())","(())()","()(())","()()()"]
12+
13+
**Example 2:**
14+
15+
**Input:** n = 1
16+
17+
**Output:** ["()"]
18+
19+
**Constraints:**
20+
21+
* `1 <= n <= 8`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0001_0100.s0023_merge_k_sorted_lists
2+
3+
import com_github_leetcode.ListNode
4+
5+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List
6+
// #Divide_and_Conquer #Merge_Sort #2022_02_18_Time_1_ms_(100.00%)_Space_43.9_MB_(75.94%)
7+
8+
class Solution {
9+
fun mergeKLists(lists: Array<ListNode>): ListNode? {
10+
return if (lists.size == 0) {
11+
null
12+
} else mergeKLists(lists, 0, lists.size)
13+
}
14+
15+
private fun mergeKLists(lists: Array<ListNode>, leftIndex: Int, rightIndex: Int): ListNode? {
16+
return if (rightIndex > leftIndex + 1) {
17+
val mid = (leftIndex + rightIndex) / 2
18+
val left = mergeKLists(lists, leftIndex, mid)
19+
val right = mergeKLists(lists, mid, rightIndex)
20+
mergeTwoLists(left, right)
21+
} else {
22+
lists[leftIndex]
23+
}
24+
}
25+
26+
private fun mergeTwoLists(left: ListNode?, right: ListNode?): ListNode? {
27+
var left = left
28+
var right = right
29+
if (left == null) {
30+
return right
31+
}
32+
if (right == null) {
33+
return left
34+
}
35+
val res: ListNode
36+
if (left.`val` <= right.`val`) {
37+
res = left
38+
left = left.next
39+
} else {
40+
res = right
41+
right = right.next
42+
}
43+
var node: ListNode? = res
44+
while (left != null || right != null) {
45+
if (left == null) {
46+
node!!.next = right
47+
right = right!!.next
48+
} else if (right == null) {
49+
node!!.next = left
50+
left = left.next
51+
} else {
52+
if (left.`val` <= right.`val`) {
53+
node!!.next = left
54+
left = left.next
55+
} else {
56+
node!!.next = right
57+
right = right.next
58+
}
59+
}
60+
node = node.next
61+
}
62+
return res
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
23\. Merge k Sorted Lists
2+
3+
Hard
4+
5+
You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.
6+
7+
_Merge all the linked-lists into one sorted linked-list and return it._
8+
9+
**Example 1:**
10+
11+
**Input:** lists = [[1,4,5],[1,3,4],[2,6]]
12+
13+
**Output:** [1,1,2,3,4,4,5,6]
14+
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
16+
17+
**Example 2:**
18+
19+
**Input:** lists = []
20+
21+
**Output:** []
22+
23+
**Example 3:**
24+
25+
**Input:** lists = [[]]
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* `k == lists.length`
32+
* `0 <= k <= 10^4`
33+
* `0 <= lists[i].length <= 500`
34+
* `-10^4 <= lists[i][j] <= 10^4`
35+
* `lists[i]` is sorted in **ascending order**.
36+
* The sum of `lists[i].length` won't exceed `10^4`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0001_0100.s0024_swap_nodes_in_pairs
2+
3+
import com_github_leetcode.ListNode
4+
5+
// #Medium #Linked_List #Recursion #2022_02_18_Time_0_ms_(100.00%)_Space_41.9_MB_(22.86%)
6+
7+
class Solution {
8+
fun swapPairs(head: ListNode?): ListNode? {
9+
if (head == null) {
10+
return null
11+
}
12+
val len = getLength(head)
13+
return reverse(head, len)
14+
}
15+
16+
private fun getLength(curr: ListNode): Int {
17+
var curr: ListNode? = curr
18+
var cnt = 0
19+
while (curr != null) {
20+
cnt++
21+
curr = curr.next
22+
}
23+
return cnt
24+
}
25+
26+
// Recursive function to reverse in groups
27+
private fun reverse(head: ListNode?, len: Int): ListNode? {
28+
// base case
29+
if (len < 2) {
30+
return head
31+
}
32+
var curr = head
33+
var prev: ListNode? = null
34+
var next: ListNode?
35+
for (i in 0..1) {
36+
// reverse linked list code
37+
next = curr!!.next
38+
curr.next = prev
39+
prev = curr
40+
curr = next
41+
}
42+
head!!.next = reverse(curr, len - 2)
43+
return prev
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
24\. Swap Nodes in Pairs
2+
3+
Medium
4+
5+
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
10+
11+
**Input:** head = [1,2,3,4]
12+
13+
**Output:** [2,1,4,3]
14+
15+
**Example 2:**
16+
17+
**Input:** head = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** head = [1]
24+
25+
**Output:** [1]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the list is in the range `[0, 100]`.
30+
* `0 <= Node.val <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0020_valid_parentheses
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isValid() {
10+
val solution = Solution()
11+
assertThat(solution.isValid("()"), equalTo(true))
12+
assertThat(solution.isValid("()[]{}"), equalTo(true))
13+
assertThat(solution.isValid("(]"), equalTo(false))
14+
assertThat(solution.isValid("([)]"), equalTo(false))
15+
assertThat(solution.isValid("{[]}"), equalTo(true))
16+
}
17+
}

0 commit comments

Comments
 (0)