Skip to content

Commit 785553f

Browse files
authored
Added tasks 385, 386, 387.
1 parent 8ae1966 commit 785553f

File tree

10 files changed

+270
-0
lines changed

10 files changed

+270
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
10821082

10831083
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10841084
|-|-|-|-|-|-
1085+
| 0387 |[First Unique Character in a String](src/main/kotlin/g0301_0400/s0387_first_unique_character_in_a_string/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Counting, Queue | 369 | 82.68
10851086
| 0383 |[Ransom Note](src/main/kotlin/g0301_0400/s0383_ransom_note/Solution.kt)| Easy | String, Hash_Table, Counting | 333 | 79.58
10861087
| 0242 |[Valid Anagram](src.save/main/kotlin/g0201_0300/s0242_valid_anagram/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Sorting | 251 | 87.65
10871088

@@ -1613,6 +1614,9 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16131614
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16141615
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
16151616
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
1617+
| 0387 |[First Unique Character in a String](src/main/kotlin/g0301_0400/s0387_first_unique_character_in_a_string/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Counting, Queue, Data_Structure_I_Day_6_String | 369 | 82.68
1618+
| 0386 |[Lexicographical Numbers](src/main/kotlin/g0301_0400/s0386_lexicographical_numbers/Solution.kt)| Medium | Depth_First_Search, Trie | 463 | 83.33
1619+
| 0385 |[Mini Parser](src/main/kotlin/g0301_0400/s0385_mini_parser/Solution.kt)| Medium | String, Depth_First_Search, Stack | 210 | 100.00
16161620
| 0384 |[Shuffle an Array](src/main/kotlin/g0301_0400/s0384_shuffle_an_array/Solution.kt)| Medium | Top_Interview_Questions, Array, Math, Randomized, Algorithm_II_Day_20_Others | 940 | 72.09
16171621
| 0383 |[Ransom Note](src/main/kotlin/g0301_0400/s0383_ransom_note/Solution.kt)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String | 333 | 79.58
16181622
| 0382 |[Linked List Random Node](src/main/kotlin/g0301_0400/s0382_linked_list_random_node/Solution.kt)| Medium | Math, Linked_List, Randomized, Reservoir_Sampling | 283 | 100.00
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g0301_0400.s0385_mini_parser
2+
3+
// #Medium #String #Depth_First_Search #Stack
4+
// #2022_11_24_Time_210_ms_(100.00%)_Space_38.3_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 Solution {
37+
private var i = 0
38+
fun deserialize(s: String): NestedInteger {
39+
return getAns(s)
40+
}
41+
42+
private fun getAns(s: String): NestedInteger {
43+
return if (s[i] == '[') {
44+
val ni = NestedInteger()
45+
i++
46+
while (i < s.length && s[i] != ']') {
47+
ni.add(getAns(s))
48+
}
49+
i++
50+
ni
51+
} else if (s[i] == ',') {
52+
i++
53+
getAns(s)
54+
} else {
55+
var x = 0
56+
var m = 1
57+
if (s[i] == '-') {
58+
i++
59+
m = -1
60+
}
61+
while (i < s.length && Character.isDigit(s[i])) {
62+
x = x * 10 + s[i++].code - '0'.code
63+
}
64+
x *= m
65+
NestedInteger(x)
66+
}
67+
}
68+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
385\. Mini Parser
2+
3+
Medium
4+
5+
Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return _the deserialized_ `NestedInteger`.
6+
7+
Each element is either an integer or a list whose elements may also be integers or other lists.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "324"
12+
13+
**Output:** 324
14+
15+
**Explanation:** You should return a NestedInteger object which contains a single integer 324.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "[123,[456,[789]]]"
20+
21+
**Output:** [123,[456,[789]]]
22+
23+
**Explanation:**
24+
Return a NestedInteger object containing a nested list with 2 elements:
25+
1. An integer containing value 123.
26+
2. A nested list containing two elements:
27+
28+
i. An integer containing value 456.
29+
30+
ii. A nested list with one element:
31+
32+
a. An integer containing value 789
33+
34+
**Constraints:**
35+
36+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
37+
* `s` consists of digits, square brackets `"[]"`, negative sign `'-'`, and commas `','`.
38+
* `s` is the serialization of valid `NestedInteger`.
39+
* All the values in the input are in the range <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code>.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0301_0400.s0386_lexicographical_numbers
2+
3+
// #Medium #Depth_First_Search #Trie #2022_11_24_Time_463_ms_(83.33%)_Space_57_MB_(33.33%)
4+
5+
class Solution {
6+
fun lexicalOrder(n: Int): List<Int> {
7+
val list = ArrayList<Int>()
8+
fun recursion(x: Int) {
9+
if (x > n) return
10+
list.add(x)
11+
for (i in 0..9) {
12+
recursion(x * 10 + i)
13+
}
14+
}
15+
for (i in 1..9) {
16+
recursion(i)
17+
}
18+
return list
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
386\. Lexicographical Numbers
2+
3+
Medium
4+
5+
Given an integer `n`, return all the numbers in the range `[1, n]` sorted in lexicographical order.
6+
7+
You must write an algorithm that runs in `O(n)` time and uses `O(1)` extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 13
12+
13+
**Output:** [1,10,11,12,13,2,3,4,5,6,7,8,9]
14+
15+
**Example 2:**
16+
17+
**Input:** n = 2
18+
19+
**Output:** [1,2]
20+
21+
**Constraints:**
22+
23+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0301_0400.s0387_first_unique_character_in_a_string
2+
3+
// #Easy #Top_Interview_Questions #String #Hash_Table #Counting #Queue
4+
// #Data_Structure_I_Day_6_String #2022_11_24_Time_369_ms_(82.68%)_Space_53.6_MB_(66.43%)
5+
6+
class Solution {
7+
fun firstUniqChar(s: String): Int {
8+
var ans = Int.MAX_VALUE
9+
var i = 'a'
10+
while (i <= 'z') {
11+
val ind = s.indexOf(i)
12+
if (ind != -1 && ind == s.lastIndexOf(i)) {
13+
ans = Math.min(ans, ind)
14+
}
15+
i++
16+
}
17+
return if (ans == Int.MAX_VALUE) {
18+
-1
19+
} else ans
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
387\. First Unique Character in a String
2+
3+
Easy
4+
5+
Given a string `s`, _find the first non-repeating character in it and return its index_. If it does not exist, return `-1`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "leetcode"
10+
11+
**Output:** 0
12+
13+
**Example 2:**
14+
15+
**Input:** s = "loveleetcode"
16+
17+
**Output:** 2
18+
19+
**Example 3:**
20+
21+
**Input:** s = "aabb"
22+
23+
**Output:** -1
24+
25+
**Constraints:**
26+
27+
* <code>1 <= s.length <= 10<sup>5</sup></code>
28+
* `s` consists of only lowercase English letters.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0301_0400.s0385_mini_parser
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 deserialize() {
10+
assertThat(Solution().deserialize("324").getInteger(), equalTo(324))
11+
}
12+
13+
@Test
14+
fun deserialize2() {
15+
val nestedInteger = Solution().deserialize("[123,[456,[789]]]")
16+
val result = intArrayOf(
17+
nestedInteger.getList()!![0].getInteger()!!,
18+
nestedInteger.getList()!![1].getList()!![0].getInteger()!!,
19+
nestedInteger.getList()!![1].getList()!![1].getList()!![0].getInteger()!!
20+
)
21+
val expected = intArrayOf(123, 456, 789)
22+
assertThat(result, equalTo(expected))
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0301_0400.s0386_lexicographical_numbers
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import java.util.Arrays
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun lexicalOrder() {
11+
assertThat(
12+
Solution().lexicalOrder(13),
13+
equalTo(Arrays.asList(1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9))
14+
)
15+
}
16+
17+
@Test
18+
fun lexicalOrder2() {
19+
assertThat(Solution().lexicalOrder(2), equalTo(Arrays.asList(1, 2)))
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0301_0400.s0387_first_unique_character_in_a_string
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 firstUniqChar() {
10+
assertThat(Solution().firstUniqChar("leetcode"), equalTo(0))
11+
}
12+
13+
@Test
14+
fun firstUniqChar2() {
15+
assertThat(Solution().firstUniqChar("loveleetcode"), equalTo(2))
16+
}
17+
18+
@Test
19+
fun firstUniqChar3() {
20+
assertThat(Solution().firstUniqChar("aabb"), equalTo(-1))
21+
}
22+
}

0 commit comments

Comments
 (0)