File tree 18 files changed +63
-27
lines changed
src/main/kotlin/g0001_0100
s0003_longest_substring_without_repeating_characters
s0004_median_of_two_sorted_arrays
s0005_longest_palindromic_substring
s0008_string_to_integer_atoi
s0010_regular_expression_matching
s0011_container_with_most_water
s0021_merge_two_sorted_lists
s0023_merge_k_sorted_lists
s0024_swap_nodes_in_pairs
18 files changed +63
-27
lines changed Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0001_two_sum
2
2
3
+ // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
4
+ // #Data_Structure_I_Day_2_Array #2022_03_22_Time_292_ms_(80.69%)_Space_41.6_MB_(60.71%)
5
+
3
6
import java.util.Arrays
4
7
5
8
class Solution {
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0002_add_two_numbers
2
2
3
+ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
4
+ // #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5
+ // #2022_03_22_Time_212_ms_(93.71%)_Space_43.1_MB_(81.36%)
6
+
3
7
import com_github_leetcode.ListNode
4
8
5
9
/*
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0003_longest_substring_without_repeating_characters
2
2
3
+ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
4
+ // #Algorithm_I_Day_6_Sliding_Window #2022_03_22_Time_212_ms_(91.24%)_Space_37_MB_(87.08%)
5
+
3
6
class Solution {
4
7
fun lengthOfLongestSubstring (s : String ): Int {
5
- val lastIndices = IntArray (256 )
6
- for (i in 0 .. 255 ) {
7
- lastIndices[i] = - 1
8
+ var i = 0
9
+ var j = 0
10
+ var longest = 0
11
+ // 1. if string empty, return 0
12
+ if (s.isEmpty()) {
13
+ return 0
8
14
}
9
- var maxLen = 0
10
- var curLen = 0
11
- var start = 0
12
- for (i in s.indices) {
13
- val cur = s[i]
14
- if (lastIndices[cur.code] < start) {
15
- lastIndices[cur.code] = i
16
- curLen++
15
+ while (j < s.length) {
16
+ // 2. if the char at index j already seen, update the longest if needs
17
+ if (i != j && s.substring(i, j).indexOf(s[j]) > - 1 ) {
18
+ longest = Math .max(j - i, longest)
19
+ i++
17
20
} else {
18
- val lastIndex = lastIndices[cur.code]
19
- start = lastIndex + 1
20
- curLen = i - start + 1
21
- lastIndices[cur.code] = i
22
- }
23
- if (curLen > maxLen) {
24
- maxLen = curLen
21
+ // 3. j out of bound already, update longest
22
+ if (++ j == s.length) {
23
+ longest = Math .max(s.length - i, longest)
24
+ break
25
+ }
25
26
}
26
27
}
27
- return maxLen
28
+ return longest
28
29
}
29
30
}
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0004_median_of_two_sorted_arrays
2
2
3
+ // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4
+ // #2022_03_22_Time_276_ms_(91.12%)_Space_47_MB_(85.71%)
5
+
3
6
import kotlin.collections.ArrayList
4
7
5
8
class Solution {
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0005_longest_palindromic_substring
2
2
3
+ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4
+ // #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5
+ // #Dynamic_Programming_I_Day_17 #2022_03_22_Time_359_ms_(69.08%)_Space_38.5_MB_(61.84%)
6
+
3
7
class Solution {
4
8
fun longestPalindrome (s : String ): String {
5
9
val newStr = CharArray (s.length * 2 + 1 )
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0006_zigzag_conversion
2
2
3
+ // #Medium #String #2022_03_22_Time_351_ms_(78.99%)_Space_40_MB_(84.02%)
4
+
3
5
class Solution {
4
6
fun convert (s : String , numRows : Int ): String {
5
7
val sLen = s.length
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0007_reverse_integer
2
2
3
+ // #Medium #Top_Interview_Questions #Math #2022_03_22_Time_230_ms_(57.36%)_Space_34.3_MB_(61.43%)
4
+
3
5
class Solution {
4
6
fun reverse (x : Int ): Int {
5
7
var rev: Long = 0
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0008_string_to_integer_atoi
2
2
3
+ // #Medium #Top_Interview_Questions #String #2022_03_22_Time_152_ms_(99.64%)_Space_35.2_MB_(97.83%)
4
+
3
5
class Solution {
4
6
fun myAtoi (str : String? ): Int {
5
7
if (str == null || str.isEmpty()) {
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0009_palindrome_number
2
2
3
+ // #Easy #Math #2022_03_22_Time_208_ms_(94.88%)_Space_35.9_MB_(90.14%)
4
+
3
5
class Solution {
4
6
fun isPalindrome (x : Int ): Boolean {
5
7
if (x < 0 ) return false
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0010_regular_expression_matching
2
2
3
+ // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion
4
+
3
5
class Solution {
4
6
private lateinit var cache: Array <Array <Boolean ?>>
5
7
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0011_container_with_most_water
2
2
3
+ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
4
+ // #2022_02_17_Time_3_ms_(91.89%)_Space_81.7_MB_(5.91%)
5
+
3
6
class Solution {
4
7
fun maxArea (height : IntArray ): Int {
5
8
var max = 0
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0012_integer_to_roman
2
2
3
+ // #Medium #String #Hash_Table #Math #2022_02_17_Time_7_ms_(70.99%)_Space_44.8_MB_(31.56%)
4
+
3
5
class Solution {
4
6
fun intToRoman (num : Int ): String? {
5
7
var localNum = num
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0013_roman_to_integer
2
2
3
+ // #Easy #Top_Interview_Questions #String #Hash_Table #Math
4
+ // #2022_02_17_Time_5_ms_(82.08%)_Space_45_MB_(29.21%)
5
+
3
6
class Solution {
4
7
fun romanToInt (s : String ): Int {
5
8
var x = 0
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0015_3sum
2
2
3
+ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
4
+ // #2022_02_17_Time_29_ms_(68.11%)_Space_59.7_MB_(23.79%)
5
+
3
6
import java.util.Arrays
4
7
import kotlin.collections.ArrayList
5
8
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0020_valid_parentheses
2
2
3
- import java.util.Stack
4
-
5
3
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
6
4
// #2022_02_17_Time_3_ms_(53.65%)_Space_42.1_MB_(17.58%)
7
5
6
+ import java.util.Stack
7
+
8
8
class Solution {
9
9
fun isValid (s : String ): Boolean {
10
10
val stack = Stack <Char >()
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0021_merge_two_sorted_lists
2
2
3
- import com_github_leetcode.ListNode
4
-
5
3
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
6
4
// #2022_02_17_Time_1_ms_(61.88%)_Space_42.8_MB_(22.76%)
7
5
6
+ import com_github_leetcode.ListNode
7
+
8
8
class Solution {
9
9
fun mergeTwoLists (l1 : ListNode ? , l2 : ListNode ? ): ListNode ? {
10
10
var l1 = l1
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0023_merge_k_sorted_lists
2
2
3
- import com_github_leetcode.ListNode
4
-
5
3
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List
6
4
// #Divide_and_Conquer #Merge_Sort #2022_02_18_Time_1_ms_(100.00%)_Space_43.9_MB_(75.94%)
7
5
6
+ import com_github_leetcode.ListNode
7
+
8
8
class Solution {
9
9
fun mergeKLists (lists : Array <ListNode >): ListNode ? {
10
10
return if (lists.size == 0 ) {
Original file line number Diff line number Diff line change 1
1
package g0001_0100.s0024_swap_nodes_in_pairs
2
2
3
- import com_github_leetcode.ListNode
4
-
5
3
// #Medium #Linked_List #Recursion #2022_02_18_Time_0_ms_(100.00%)_Space_41.9_MB_(22.86%)
6
4
5
+ import com_github_leetcode.ListNode
6
+
7
7
class Solution {
8
8
fun swapPairs (head : ListNode ? ): ListNode ? {
9
9
if (head == null ) {
You can’t perform that action at this time.
0 commit comments