Skip to content

Commit 8ae1966

Browse files
authored
Added task 341.
1 parent 806c040 commit 8ae1966

File tree

8 files changed

+199
-2
lines changed

8 files changed

+199
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
479479
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
480480
|-|-|-|-|-|-
481481
| 0155 |[Min Stack](src.save/main/kotlin/g0101_0200/s0155_min_stack/MinStack.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design | 331 | 84.88
482+
| 0341 |[Flatten Nested List Iterator](src/main/kotlin/g0301_0400/s0341_flatten_nested_list_iterator/NestedIterator.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Tree, Stack, Design, Queue, Iterator | 210 | 100.00
482483

483484
#### Day 19
484485

@@ -1528,7 +1529,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
15281529

15291530
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15301531
|-|-|-|-|-|-
1531-
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/Solution.kt)| Easy | Binary_Search, Interactive, Binary_Search_I_Day_1 | 134 | 94.19
1532+
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/Solution.kt)| Easy | Binary_Search, Interactive | 134 | 94.19
15321533

15331534
#### Day 2
15341535

@@ -1640,6 +1641,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16401641
| 0344 |[Reverse String](src/main/kotlin/g0301_0400/s0344_reverse_string/Solution.kt)| Easy | Top_Interview_Questions, String, Two_Pointers, Recursion, Algorithm_I_Day_4_Two_Pointers, Udemy_Strings | 445 | 69.75
16411642
| 0343 |[Integer Break](src/main/kotlin/g0301_0400/s0343_integer_break/Solution.kt)| Medium | Dynamic_Programming, Math, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_21 | 218 | 63.89
16421643
| 0342 |[Power of Four](src/main/kotlin/g0301_0400/s0342_power_of_four/Solution.kt)| Easy | Math, Bit_Manipulation, Recursion | 150 | 92.11
1644+
| 0341 |[Flatten Nested List Iterator](src/main/kotlin/g0301_0400/s0341_flatten_nested_list_iterator/NestedIterator.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Tree, Stack, Design, Queue, Iterator, Programming_Skills_II_Day_18 | 210 | 100.00
16431645
| 0338 |[Counting Bits](src/main/kotlin/g0301_0400/s0338_counting_bits/Solution.kt)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 186 | 99.26
16441646
| 0337 |[House Robber III](src/main/kotlin/g0301_0400/s0337_house_robber_iii/Solution.kt)| Medium | Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 282 | 84.62
16451647
| 0336 |[Palindrome Pairs](src/main/kotlin/g0301_0400/s0336_palindrome_pairs/Solution.kt)| Hard | Array, String, Hash_Table, Trie | 2451 | 67.33
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com_github_leetcode
2+
3+
class NestedInteger {
4+
private var list: MutableList<NestedInteger>? = null
5+
private var integer: Int? = null
6+
7+
constructor() {
8+
list = ArrayList()
9+
}
10+
11+
constructor(list: MutableList<NestedInteger>?) {
12+
this.list = list
13+
}
14+
15+
constructor(integer: Int?) {
16+
this.integer = integer
17+
}
18+
19+
fun isInteger(): Boolean {
20+
return integer != null
21+
}
22+
23+
fun getInteger(): Int? {
24+
return integer
25+
}
26+
27+
fun getList(): List<NestedInteger>? {
28+
return list
29+
}
30+
31+
fun add(nestedInteger: NestedInteger) {
32+
list!!.add(nestedInteger)
33+
}
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0301_0400.s0341_flatten_nested_list_iterator
2+
3+
// #Medium #Top_Interview_Questions #Depth_First_Search #Tree #Stack #Design #Queue #Iterator
4+
// #Programming_Skills_II_Day_18 #2022_11_25_Time_210_ms_(100.00%)_Space_37.6_MB_(100.00%)
5+
6+
import com_github_leetcode.NestedInteger
7+
8+
/*
9+
* // This is the interface that allows for creating nested lists.
10+
* // You should not implement it, or speculate about its implementation
11+
* class NestedInteger {
12+
* // Constructor initializes an empty nested list.
13+
* constructor()
14+
*
15+
* // Constructor initializes a single integer.
16+
* constructor(value: Int)
17+
*
18+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
19+
* fun isInteger(): Boolean
20+
*
21+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
22+
* // Return null if this NestedInteger holds a nested list
23+
* fun getInteger(): Int?
24+
*
25+
* // Set this NestedInteger to hold a single integer.
26+
* fun setInteger(value: Int): Unit
27+
*
28+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
29+
* fun add(ni: NestedInteger): Unit
30+
*
31+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
32+
* // Return null if this NestedInteger holds a single integer
33+
* fun getList(): List<NestedInteger>?
34+
* }
35+
*/
36+
class NestedIterator(nestedList: List<NestedInteger>) {
37+
private var flattenList = mutableListOf<Int>()
38+
private var index = 0
39+
40+
init {
41+
flatten(nestedList, flattenList)
42+
}
43+
44+
private fun flatten(nestedList: List<NestedInteger>, flattenList: MutableList<Int>) {
45+
nestedList.forEach { nestedInteger ->
46+
if (nestedInteger.isInteger()) {
47+
flattenList.add(nestedInteger.getInteger()!!)
48+
} else {
49+
flatten(nestedInteger.getList()!!, flattenList)
50+
}
51+
}
52+
}
53+
54+
fun next(): Int = flattenList[index++]
55+
56+
fun hasNext(): Boolean = index < flattenList.size
57+
}
58+
59+
/*
60+
* Your NestedIterator object will be instantiated and called as such:
61+
* var obj = NestedIterator(nestedList)
62+
* var param_1 = obj.next()
63+
* var param_2 = obj.hasNext()
64+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
341\. Flatten Nested List Iterator
2+
3+
Medium
4+
5+
You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
6+
7+
Implement the `NestedIterator` class:
8+
9+
* `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
10+
* `int next()` Returns the next integer in the nested list.
11+
* `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.
12+
13+
Your code will be tested with the following pseudocode:
14+
15+
initialize iterator with nestedList
16+
res = []
17+
while iterator.hasNext()
18+
append iterator.next() to the end of res
19+
return res
20+
21+
If `res` matches the expected flattened list, then your code will be judged as correct.
22+
23+
**Example 1:**
24+
25+
**Input:** nestedList = [[1,1],2,[1,1]]
26+
27+
**Output:** [1,1,2,1,1]
28+
29+
**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
30+
31+
**Example 2:**
32+
33+
**Input:** nestedList = [1,[4,[6]]]
34+
35+
**Output:** [1,4,6]
36+
37+
**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
38+
39+
**Constraints:**
40+
41+
* `1 <= nestedList.length <= 500`
42+
* The values of the integers in the nested list is in the range <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code>.

src/main/kotlin/g0301_0400/s0371_sum_of_two_integers/Solution.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package g0301_0400.s0371_sum_of_two_integers
33
// #Medium #Top_Interview_Questions #Math #Bit_Manipulation #Udemy_Bit_Manipulation
44
// #2022_11_22_Time_129_ms_(95.45%)_Space_32.9_MB_(90.91%)
55

6+
@Suppress("NAME_SHADOWING")
67
class Solution {
78
fun getSum(a: Int, b: Int): Int {
89
var a = a

src/main/kotlin/g0301_0400/s0372_super_pow/Solution.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package g0301_0400.s0372_super_pow
22

33
// #Medium #Math #Divide_and_Conquer #2022_11_22_Time_196_ms_(100.00%)_Space_36.9_MB_(100.00%)
44

5+
@Suppress("NAME_SHADOWING")
56
class Solution {
67
fun superPow(a: Int, b: IntArray): Int {
78
val phi = phi(MOD)

src/main/kotlin/g0301_0400/s0382_linked_list_random_node/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.util.Random
1515
* var next: ListNode? = null
1616
* }
1717
*/
18-
@Suppress("kotlin:S2245")
18+
@Suppress("NAME_SHADOWING", "kotlin:S2245")
1919
class Solution(head: ListNode?) {
2020
private val al: MutableList<Int>
2121
private val rand: Random
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0301_0400.s0341_flatten_nested_list_iterator
2+
3+
import com_github_leetcode.NestedInteger
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class NestedIteratorTest {
9+
@Test
10+
fun nestedIterator() {
11+
val iterator = NestedIterator(
12+
mutableListOf(
13+
NestedInteger(
14+
mutableListOf(NestedInteger(1), NestedInteger(1))
15+
),
16+
NestedInteger(2),
17+
NestedInteger(
18+
mutableListOf(
19+
NestedInteger(1), NestedInteger(1)
20+
)
21+
)
22+
)
23+
)
24+
val result: MutableList<Int> = ArrayList()
25+
while (iterator.hasNext()) {
26+
result.add(iterator.next())
27+
}
28+
assertThat(result, equalTo(listOf(1, 1, 2, 1, 1)))
29+
}
30+
31+
@Test
32+
fun nestedIterator2() {
33+
val integer1 = NestedInteger(1)
34+
val integer2 = NestedInteger(2)
35+
val integer3 = NestedInteger(3)
36+
val list: MutableList<NestedInteger> = ArrayList()
37+
list.add(integer1)
38+
list.add(integer2)
39+
list.add(integer3)
40+
val nestedIntegerList = NestedInteger(list)
41+
val input: MutableList<NestedInteger> = ArrayList()
42+
input.add(integer1)
43+
input.add(integer2)
44+
input.add(nestedIntegerList)
45+
input.add(integer3)
46+
val iterator = NestedIterator(input)
47+
val result: MutableList<Int> = ArrayList()
48+
while (iterator.hasNext()) {
49+
result.add(iterator.next())
50+
}
51+
assertThat(result, equalTo(listOf(1, 2, 1, 2, 3, 3)))
52+
}
53+
}

0 commit comments

Comments
 (0)