File tree Expand file tree Collapse 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 Expand file tree Collapse file tree 18 files changed +63
-27
lines changed Original file line number Diff line number Diff line change 11package g0001_0100.s0001_two_sum
22
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+
36import java.util.Arrays
47
58class Solution {
Original file line number Diff line number Diff line change 11package g0001_0100.s0002_add_two_numbers
22
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+
37import com_github_leetcode.ListNode
48
59/*
Original file line number Diff line number Diff line change 11package g0001_0100.s0003_longest_substring_without_repeating_characters
22
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+
36class Solution {
47 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
814 }
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++
1720 } 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+ }
2526 }
2627 }
27- return maxLen
28+ return longest
2829 }
2930}
Original file line number Diff line number Diff line change 11package g0001_0100.s0004_median_of_two_sorted_arrays
22
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+
36import kotlin.collections.ArrayList
47
58class Solution {
Original file line number Diff line number Diff line change 11package g0001_0100.s0005_longest_palindromic_substring
22
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+
37class Solution {
48 fun longestPalindrome (s : String ): String {
59 val newStr = CharArray (s.length * 2 + 1 )
Original file line number Diff line number Diff line change 11package g0001_0100.s0006_zigzag_conversion
22
3+ // #Medium #String #2022_03_22_Time_351_ms_(78.99%)_Space_40_MB_(84.02%)
4+
35class Solution {
46 fun convert (s : String , numRows : Int ): String {
57 val sLen = s.length
Original file line number Diff line number Diff line change 11package g0001_0100.s0007_reverse_integer
22
3+ // #Medium #Top_Interview_Questions #Math #2022_03_22_Time_230_ms_(57.36%)_Space_34.3_MB_(61.43%)
4+
35class Solution {
46 fun reverse (x : Int ): Int {
57 var rev: Long = 0
Original file line number Diff line number Diff line change 11package g0001_0100.s0008_string_to_integer_atoi
22
3+ // #Medium #Top_Interview_Questions #String #2022_03_22_Time_152_ms_(99.64%)_Space_35.2_MB_(97.83%)
4+
35class Solution {
46 fun myAtoi (str : String? ): Int {
57 if (str == null || str.isEmpty()) {
Original file line number Diff line number Diff line change 11package g0001_0100.s0009_palindrome_number
22
3+ // #Easy #Math #2022_03_22_Time_208_ms_(94.88%)_Space_35.9_MB_(90.14%)
4+
35class Solution {
46 fun isPalindrome (x : Int ): Boolean {
57 if (x < 0 ) return false
Original file line number Diff line number Diff line change 11package g0001_0100.s0010_regular_expression_matching
22
3+ // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion
4+
35class Solution {
46 private lateinit var cache: Array <Array <Boolean ?>>
57
You can’t perform that action at this time.
0 commit comments