Skip to content

Commit b41d0fb

Browse files
authored
Added tasks 12-27
1 parent 3ace9e4 commit b41d0fb

File tree

16 files changed

+547
-0
lines changed

16 files changed

+547
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
655655

656656
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
657657
|-|-|-|-|-|-
658+
| 0014 |[Longest Common Prefix](src/main/go/g0001_0100/s0014_longest_common_prefix/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String | 0 | 100.00
658659

659660
#### Day 3 Linked List
660661

@@ -781,6 +782,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
781782

782783
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
783784
|-|-|-|-|-|-
785+
| 0014 |[Longest Common Prefix](src/main/go/g0001_0100/s0014_longest_common_prefix/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String | 0 | 100.00
784786
| 0003 |[Longest Substring Without Repeating Characters](src/main/go/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 0 | 100.00
785787
| 0020 |[Valid Parentheses](src/main/go/g0001_0100/s0020_valid_parentheses/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
786788
| 0005 |[Longest Palindromic Substring](src/main/go/g0001_0100/s0005_longest_palindromic_substring/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
@@ -812,6 +814,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
812814

813815
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
814816
|-|-|-|-|-|-
817+
| 0026 |[Remove Duplicates from Sorted Array](src/main/go/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.go)| Easy | Top_Interview_Questions, Array, Two_Pointers | 0 | 100.00
815818
| 0042 |[Trapping Rain Water](src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
816819
| 0015 |[3Sum](src/main/go/g0001_0100/s0015_3sum/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 12 | 99.83
817820

@@ -919,13 +922,18 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
919922

920923
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
921924
|-|-|-|-|-|-
925+
| 0027 |[Remove Element](src/main/go/g0001_0100/s0027_remove_element/solution.go)| Easy | Array, Two_Pointers | 0 | 100.00
926+
| 0026 |[Remove Duplicates from Sorted Array](src/main/go/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.go)| Easy | Top_Interview_Questions, Array, Two_Pointers | 0 | 100.00
922927
| 0169 |[Majority Element](src/main/go/g0101_0200/s0169_majority_element/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
923928
| 0189 |[Rotate Array](src/main/go/g0101_0200/s0189_rotate_array/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
924929
| 0121 |[Best Time to Buy and Sell Stock](src/main/go/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
925930
| 0055 |[Jump Game](src/main/go/g0001_0100/s0055_jump_game/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
926931
| 0045 |[Jump Game II](src/main/go/g0001_0100/s0045_jump_game_ii/solution.go)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
927932
| 0238 |[Product of Array Except Self](src/main/go/g0201_0300/s0238_product_of_array_except_self/solution.go)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, LeetCode_75_Array/String, Big_O_Time_O(n^2)_Space_O(n) | 0 | 100.00
928933
| 0042 |[Trapping Rain Water](src/main/go/g0001_0100/s0042_trapping_rain_water/solution.go)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
934+
| 0013 |[Roman to Integer](src/main/go/g0001_0100/s0013_roman_to_integer/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Math | 0 | 100.00
935+
| 0012 |[Integer to Roman](src/main/go/g0001_0100/s0012_integer_to_roman/solution.go)| Medium | String, Hash_Table, Math | 0 | 100.00
936+
| 0014 |[Longest Common Prefix](src/main/go/g0001_0100/s0014_longest_common_prefix/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String | 0 | 100.00
929937
| 0006 |[Zigzag Conversion](src/main/go/g0001_0100/s0006_zigzag_conversion/solution.go)| Medium | String | 0 | 100.00
930938

931939
#### Top Interview 150 Two Pointers
@@ -1680,6 +1688,8 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
16801688
| 0033 |[Search in Rotated Sorted Array](src/main/go/g0001_0100/s0033_search_in_rotated_sorted_array/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Top_Interview_150_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
16811689
| 0032 |[Longest Valid Parentheses](src/main/go/g0001_0100/s0032_longest_valid_parentheses/solution.go)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
16821690
| 0031 |[Next Permutation](src/main/go/g0001_0100/s0031_next_permutation/solution.go)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1691+
| 0027 |[Remove Element](src/main/go/g0001_0100/s0027_remove_element/solution.go)| Easy | Array, Two_Pointers, Top_Interview_150_Array/String | 0 | 100.00
1692+
| 0026 |[Remove Duplicates from Sorted Array](src/main/go/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.go)| Easy | Top_Interview_Questions, Array, Two_Pointers, Udemy_Two_Pointers, Top_Interview_150_Array/String | 0 | 100.00
16831693
| 0025 |[Reverse Nodes in k-Group](src/main/go/g0001_0100/s0025_reverse_nodes_in_k_group/solution.go)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
16841694
| 0024 |[Swap Nodes in Pairs](src/main/go/g0001_0100/s0024_swap_nodes_in_pairs/solution.go)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
16851695
| 0023 |[Merge k Sorted Lists](src/main/go/g0001_0100/s0023_merge_k_sorted_lists/solution.go)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Top_Interview_150_Divide_and_Conquer, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 0 | 100.00
@@ -1689,6 +1699,9 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
16891699
| 0019 |[Remove Nth Node From End of List](src/main/go/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Level_2_Day_3_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
16901700
| 0017 |[Letter Combinations of a Phone Number](src/main/go/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, LeetCode_75_Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Top_Interview_150_Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
16911701
| 0015 |[3Sum](src/main/go/g0001_0100/s0015_3sum/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_1_Array, Algorithm_II_Day_3_Two_Pointers, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 12 | 99.83
1702+
| 0014 |[Longest Common Prefix](src/main/go/g0001_0100/s0014_longest_common_prefix/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Level_2_Day_2_String, Udemy_Strings, Top_Interview_150_Array/String | 0 | 100.00
1703+
| 0013 |[Roman to Integer](src/main/go/g0001_0100/s0013_roman_to_integer/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Math, Top_Interview_150_Array/String | 0 | 100.00
1704+
| 0012 |[Integer to Roman](src/main/go/g0001_0100/s0012_integer_to_roman/solution.go)| Medium | String, Hash_Table, Math, Top_Interview_150_Array/String | 0 | 100.00
16921705
| 0011 |[Container With Most Water](src/main/go/g0001_0100/s0011_container_with_most_water/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, LeetCode_75_Two_Pointers, Algorithm_II_Day_4_Two_Pointers, Top_Interview_150_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
16931706
| 0010 |[Regular Expression Matching](src/main/go/g0001_0100/s0010_regular_expression_matching/solution.go)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
16941707
| 0009 |[Palindrome Number](src/main/go/g0001_0100/s0009_palindrome_number/solution.go)| Easy | Math, Udemy_Integers, Top_Interview_150_Math | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
12\. Integer to Roman
2+
3+
Medium
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given an integer, convert it to a roman numeral.
25+
26+
**Example 1:**
27+
28+
**Input:** num = 3
29+
30+
**Output:** "III"
31+
32+
**Example 2:**
33+
34+
**Input:** num = 4
35+
36+
**Output:** "IV"
37+
38+
**Example 3:**
39+
40+
**Input:** num = 9
41+
42+
**Output:** "IX"
43+
44+
**Example 4:**
45+
46+
**Input:** num = 58
47+
48+
**Output:** "LVIII"
49+
50+
**Explanation:** L = 50, V = 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** num = 1994
55+
56+
**Output:** "MCMXCIV"
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= num <= 3999`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package s0012_integer_to_roman
2+
3+
// #Medium #String #Hash_Table #Math #Top_Interview_150_Array/String
4+
// #2025_05_12_Time_0_ms_(100.00%)_Space_4.94_MB_(97.25%)
5+
6+
import (
7+
"strings"
8+
)
9+
10+
func intToRoman(num int) string {
11+
var result strings.Builder
12+
m := 1000
13+
c := 100
14+
x := 10
15+
i := 1
16+
17+
num = numerals(&result, num, m, ' ', ' ', 'M')
18+
num = numerals(&result, num, c, 'M', 'D', 'C')
19+
num = numerals(&result, num, x, 'C', 'L', 'X')
20+
numerals(&result, num, i, 'X', 'V', 'I')
21+
22+
return result.String()
23+
}
24+
25+
func numerals(sb *strings.Builder, num, one int, cTen, cFive, cOne rune) int {
26+
div := num / one
27+
switch div {
28+
case 9:
29+
sb.WriteRune(cOne)
30+
sb.WriteRune(cTen)
31+
case 8:
32+
sb.WriteRune(cFive)
33+
sb.WriteRune(cOne)
34+
sb.WriteRune(cOne)
35+
sb.WriteRune(cOne)
36+
case 7:
37+
sb.WriteRune(cFive)
38+
sb.WriteRune(cOne)
39+
sb.WriteRune(cOne)
40+
case 6:
41+
sb.WriteRune(cFive)
42+
sb.WriteRune(cOne)
43+
case 5:
44+
sb.WriteRune(cFive)
45+
case 4:
46+
sb.WriteRune(cOne)
47+
sb.WriteRune(cFive)
48+
case 3:
49+
sb.WriteRune(cOne)
50+
sb.WriteRune(cOne)
51+
sb.WriteRune(cOne)
52+
case 2:
53+
sb.WriteRune(cOne)
54+
sb.WriteRune(cOne)
55+
case 1:
56+
sb.WriteRune(cOne)
57+
}
58+
return num - (div * one)
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package s0012_integer_to_roman
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestIntToRoman(t *testing.T) {
9+
assert.Equal(t, "III", intToRoman(3))
10+
}
11+
12+
func TestIntToRoman2(t *testing.T) {
13+
assert.Equal(t, "IV", intToRoman(4))
14+
}
15+
16+
func TestIntToRoman3(t *testing.T) {
17+
assert.Equal(t, "IX", intToRoman(9))
18+
}
19+
20+
func TestIntToRoman4(t *testing.T) {
21+
assert.Equal(t, "LVIII", intToRoman(58))
22+
}
23+
24+
func TestIntToRoman5(t *testing.T) {
25+
assert.Equal(t, "MCMXCIV", intToRoman(1994))
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
13\. Roman to Integer
2+
3+
Easy
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
Symbol Value
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
16+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
17+
18+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
19+
20+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
21+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
22+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
23+
24+
Given a roman numeral, convert it to an integer.
25+
26+
**Example 1:**
27+
28+
**Input:** s = "III"
29+
30+
**Output:** 3
31+
32+
**Example 2:**
33+
34+
**Input:** s = "IV"
35+
36+
**Output:** 4
37+
38+
**Example 3:**
39+
40+
**Input:** s = "IX"
41+
42+
**Output:** 9
43+
44+
**Example 4:**
45+
46+
**Input:** s = "LVIII"
47+
48+
**Output:** 58
49+
50+
**Explanation:** L = 50, V= 5, III = 3.
51+
52+
**Example 5:**
53+
54+
**Input:** s = "MCMXCIV"
55+
56+
**Output:** 1994
57+
58+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
59+
60+
**Constraints:**
61+
62+
* `1 <= s.length <= 15`
63+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
64+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package s0013_roman_to_integer
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Math
4+
// #Top_Interview_150_Array/String #2025_05_12_Time_0_ms_(100.00%)_Space_4.73_MB_(58.29%)
5+
6+
func romanToInt(s string) int {
7+
x := 0
8+
for i := 0; i < len(s); i++ {
9+
switch s[i] {
10+
case 'I':
11+
x = getX(s, x, i, 1, 'V', 'X')
12+
case 'V':
13+
x += 5
14+
case 'X':
15+
x = getX(s, x, i, 10, 'L', 'C')
16+
case 'L':
17+
x += 50
18+
case 'C':
19+
x = getX(s, x, i, 100, 'D', 'M')
20+
case 'D':
21+
x += 500
22+
case 'M':
23+
x += 1000
24+
}
25+
}
26+
return x
27+
}
28+
29+
func getX(s string, x, i, i2 int, v, x2 byte) int {
30+
if i+1 == len(s) {
31+
x += i2
32+
} else if s[i+1] == v {
33+
x -= i2
34+
} else if s[i+1] == x2 {
35+
x -= i2
36+
} else {
37+
x += i2
38+
}
39+
return x
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0013_roman_to_integer
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestRomanToInt(t *testing.T) {
9+
assert.Equal(t, 3, romanToInt("III"))
10+
}
11+
12+
func TestRomanToInt2(t *testing.T) {
13+
assert.Equal(t, 58, romanToInt("LVIII"))
14+
}
15+
16+
func TestRomanToInt3(t *testing.T) {
17+
assert.Equal(t, 1994, romanToInt("MCMXCIV"))
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
14\. Longest Common Prefix
2+
3+
Easy
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string `""`.
8+
9+
**Example 1:**
10+
11+
**Input:** strs = ["flower","flow","flight"]
12+
13+
**Output:** "fl"
14+
15+
**Example 2:**
16+
17+
**Input:** strs = ["dog","racecar","car"]
18+
19+
**Output:** ""
20+
21+
**Explanation:** There is no common prefix among the input strings.
22+
23+
**Constraints:**
24+
25+
* `1 <= strs.length <= 200`
26+
* `0 <= strs[i].length <= 200`
27+
* `strs[i]` consists of only lower-case English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0014_longest_common_prefix
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Level_2_Day_2_String
4+
// #Udemy_Strings #Top_Interview_150_Array/String
5+
// #2025_05_12_Time_0_ms_(100.00%)_Space_4.28_MB_(73.97%)
6+
7+
func longestCommonPrefix(strs []string) string {
8+
if len(strs) < 1 {
9+
return ""
10+
}
11+
if len(strs) == 1 {
12+
return strs[0]
13+
}
14+
temp := strs[0]
15+
i := 1
16+
for temp != "" && i < len(strs) {
17+
if len(temp) > len(strs[i]) {
18+
temp = temp[:len(strs[i])]
19+
}
20+
cur := strs[i][:len(temp)]
21+
if cur != temp {
22+
temp = temp[:len(temp)-1]
23+
} else {
24+
i++
25+
}
26+
}
27+
return temp
28+
}

0 commit comments

Comments
 (0)