-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
293 lines (267 loc) · 21.7 KB
/
index.html
File metadata and controls
293 lines (267 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCodeSulotions</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer=""></script>
</head>
<body>
<section class="header-section">
<div class="header">
<h1 id="main-title"><i><a title="6gDav GitHub" href="https://github.com/6gDav">6gDav</a></i> <span id="after-name-text">LeetCode
Sulotions</span></h1>
<p id="second-title">Welcome to my LeetCode solutions collection!</p>
<svg class="waves" xmlns="http://www.w3.org/2000/svg" viewBox="0 24 150 28" preserveAspectRatio="none">
<defs>
<path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z"></path>
</defs>
<g class="parallax">
<use href="#gentle-wave" x="48" y="0" fill="rgba(255,255,255,0.7"></use>
<use href="#gentle-wave" x="48" y="3" fill="rgba(255,255,255,0.5)"></use>
<use href="#gentle-wave" x="48" y="5" fill="rgba(255,255,255,0.3)"></use>
<use href="#gentle-wave" x="48" y="7" fill="#fff"></use>
</g>
</svg>
</div>
</section>
<main>
<section class="description-section">
<div>
<h2>Description</h2>
<div id="description-div">
<p>
This is a collection page for my LeetCode solutions. <br>
I created this because GitHub’s landing page lacks a "pinning" option (for all my repos), <br>
and I wanted a place to organize my code. <br>
I mainly solve LeetCode problems for practice and to learn complex algorithms, rather than for
job interviews.
</p>
</div>
<br>
<h3 id="filter-section">
Choose a LeetCode:
<select id="filter-options">
<option value="" disabled="" selected="">Problem</option>
<option value="leetcode1">LeetCode 1. </option>
<option value="leetcode2">LeetCode 2. </option>
<option value="leetcode4">LeetCode 4. </option>
<option value="leetcode5">LeetCode 5. </option>
<option value="leetcode54">LeetCode 54. </option>
<option value="leetcode6">LeetCode 6. </option>
<option value="leetcode4.2">LeetCode 4.2. </option>
<option value="leetcode7">LeetCode 7. </option>
<option value="leetcode9">LeetCode 9. </option>
<option value="leetcode8">LeetCode 8. </option>
<option value="leetcode7.2">LeetCode 7.2. </option>
<option value="leetcode1.2">LeetCode 1.2. </option>
<option value="leetcode11">LeetCode 11. </option>
<option value="leetcode11.2">LeetCode 11.2. </option>
<option value="leetcode12">LeetCode 12. </option>
<option value="leetcode12.2">LeetCode 12.2. </option>
</select>
</h3>
<section id="leetcode1" class="leetcode-sulotions-container">
<h2>LeetCode 1.</h2>
<h3><a href="https://leetcode.com/problems/two-sum/description/" target="_blank" title="LeetCode Link">Two Sum</a> ➕</h3>
<h3>Programing langeuge: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_1_no1" target="_blank">My sulotions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I implemented a brute-force solution using nested loops. <br>
To avoid the "double-counting" bug (using the same element twice), <br>
the inner loop starts from the next element relative to the outer loop. <br>
This ensures that we only compare distinct pairs and find the target sum <br>
without using any element more than once. <br>
</p>
</section>
<br>
<section id="leetcode2" class="leetcode-sulotions-container">
<h2>LeetCode 2.</h2>
<h3><a href="https://leetcode.com/problems/add-two-numbers/description/" target="_blank" title="LeetCode Link">Add Two Numbers</a> x ➕ ⅹ</h3>
<h3>Programing langeuge: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_2_no1" target="_blank">My sulotions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I reversed both input lists, then iterated through the elements and <br>
converted them to strings. After concatenating them, I parsed the final <br>
string back into an i32 to get the full number. <br>
I did this for both lists, added the two numbers together, <br>
and finally converted the sum back to a reversed list of characters. <br>
</p>
</section>
<br>
<section id="leetcode4" class="leetcode-sulotions-container">
<h2>LeetCode 4.</h2>
<h3><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/description/" target="_blank" title="LeetCode Link">Median of Two Sorted Arrays</a> 🖖</h3>
<h3>Programing langeuge: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_4_no1" target="_blank">My sulotions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First, I concatenated and sorted the given lists. <br>
Then, I implemented the following logic: if the combined list length is even, <br>
I split it in the middle and calculate the average of the two central elements. <br>
If the length is odd, I simply return the middle element. <br>
</p>
</section>
<br>
<section id="leetcode5" class="leetcode-sulotions-container">
<h2>LeetCode 5.</h2>
<h3><a href="https://leetcode.com/problems/longest-palindromic-substring/description/" target="_blank" title="LeetCode Link">Longest Palindromic Substring</a> 🤥</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_5_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First, I return the original string if it is equal to its reversed version to avoid unnecessary processing. After this, I implemented a for loop that continuously compares a new string with its own reversed version. The backward logic follows the same principle. The last one is a bit of a silly attempt, but I'll leave it in.
</p>
</section>
<br>
<section id="leetcode54" class="leetcode-sulotions-container">
<h2>LeetCode 54.</h2>
<h3><a href="https://leetcode.com/problems/spiral-matrix/description/" target="_blank" title="LeetCode Link">Spiral Matrix</a> ⊹</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_54_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I created a while loop that runs until the matrix becomes empty or the first row runs out. In the while loop I implemented four steps: 1. remove the current first row and add it to the solution vector 2. iterate through all rows and remove all last elements while pushing these to the vector 3. remove the entire last row reverse it and add it too to the solution 4. add every first element while remove these too As you see my solution based on remove and cloning.
</p>
</section>
<br>
<section id="leetcode6" class="leetcode-sulotions-container">
<h2>LeetCode 6.</h2>
<h3><a href="https://leetcode.com/problems/zigzag-conversion/description/" target="_blank" title="LeetCode Link">Zigzag Conversion</a> ↯</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_6_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First, I implemented an early return for cases with only a single character. Then, I created an array of strings (acting as a matrix) based on the number of rows. I iterated through the input, pushing each character into the appropriate row index. The logic relies on the 'step' variable that increments or decrements the 'current_row' index. This 'step' changes whenever we reach the top or bottom row, determining where the next character is placed.
</p>
</section>
<br>
<section id="leetcode4.2" class="leetcode-sulotions-container">
<h2>LeetCode 4.2.</h2>
<h3><a href="https://leetcode.com/problems/median-of-two-sorted-arrays/description/" target="_blank" title="LeetCode Link">Median of Two Sorted Arrays</a> 🖖</h3>
<h3>Programming language: <a href="https://dart.dev/docs" target="_blank" title="Dart lang Link">Dart</a> 🎯</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_4_no1_2" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First, I concatenated and sorted the given lists. Then, I implemented the following logic: if the combined list length is even, I split it in the middle and calculate the average of the two central elements. If the length is odd, I simply return the middle element.
</p>
</section>
<br>
<section id="leetcode7" class="leetcode-sulotions-container">
<h2>LeetCode 7.</h2>
<h3><a href="https://leetcode.com/problems/reverse-integer/description/" target="_blank" title="LeetCode Link">Reverse Integer</a> 🔄</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_7_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I implemented an if-else expression with two branches. In the first case, if the input is negative, I take its absolute value, convert it to a string, reverse it, and return it to the variable. The else block does the same but without the absolute value. Finally, I parsed the resulting string back into an integer.
</p>
</section>
<br>
<section id="leetcode9" class="leetcode-sulotions-container">
<h2>LeetCode 9.</h2>
<h3><a href="https://leetcode.com/problems/palindrome-number/description/" target="_blank" title="LeetCode Link">Palindrome Number</a> 6️⃣7️⃣</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_9_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I implemented a string-based solution. First, I converted the number to a string for easier handling. After this, I used an early return to return true if the string is only one character long. Finally, I return a boolean based on whether the string matches its reversed version.
</p>
</section>
<br>
<section id="leetcode8" class="leetcode-sulotions-container">
<h2>LeetCode 8.</h2>
<h3><a href="https://leetcode.com/problems/string-to-integer-atoi/description/" target="_blank" title="LeetCode Link">String to Integer (atoi)</a> ꦄ</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_8_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First of all, I broke down the "Atoi" conversion behavior into pieces. The first piece was removing the unnecessary whitespace from the very beginning of the string by creating a new string. After this, I implemented an early return statement if the new string is empty. I check the first character of the new string: if it is a non-digit character and not '+' or '-', I return a falsy value, which is 0. I implemented another check for the first element to filter out sign duplications. If the sign is duplicated (like '++'), I return 0 as well. I iterate through the new string and examine all characters. If the character is a number, I add it to the newly created variable. If the character is not a number (even if it's a sign in this case), I break out of the loop. The next step is the type casting. I created 4 variables: two of them help with the casting, and the others are for the state. I iterate through the string where the numbers are stored and push each element to another string. I try to convert this string to a 64-bit integer. If it's successful, I check the previously declared sign boolean. If it's true, I check if the converted integer is larger than the maximum value of a 32-bit integer; if so, I return the maximum 32-bit integer. In the case of a negative number, I do the same but with the minimum of a 32-bit integer. Finally, I parse the successfully converted value to a 32-bit integer and return it.
</p>
</section>
<br>
<section id="leetcode7.2" class="leetcode-sulotions-container">
<h2>LeetCode 7.2.</h2>
<h3><a href="https://leetcode.com/problems/reverse-integer/" target="_blank" title="LeetCode Link">Reverse Integer</a> 🔄</h3>
<h3>Programming language: <a href="https://go.dev/" target="_blank" title="Golang lang Link">Golang</a> 🐹</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_7_no1_2" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I implemented an if-else expression with two branches. In the first case, if the input is negative, I take its absolute value, convert it to a string, reverse it, and return it to the variable. The else block does the same but without the absolute value. Finally, I parsed the resulting string back into an integer.
</p>
</section>
<br>
<section id="leetcode1.2" class="leetcode-sulotions-container">
<h2>LeetCode 1.2.</h2>
<h3><a href="https://leetcode.com/problems/two-sum/description/" target="_blank" title="LeetCode Link">Two Sum</a> ➕</h3>
<h3>Programming language: <a href="https://go.dev/" target="_blank" title="Golang lang Link">Golang</a> 🐹</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_1_no1_2" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I implemented a brute-force solution using nested loops. To avoid the "double-counting" bug (using the same element twice), I implemted a condition that continue the loop if the "indexes" are equal with eacother. This ensures that we only compare distinct pairs and find the target sum without using any element more than once.
</p>
</section>
<br>
<section id="leetcode11" class="leetcode-sulotions-container">
<h2>LeetCode 11.</h2>
<h3><a href="https://leetcode.com/problems/container-with-most-water/description/" target="_blank" title="LeetCode Link">Container With Most Water</a> 🥛</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_11_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I used a two-pointer based solution, so I iterate through the given list from the left and the right sides. I implemented an infinite loop that breaks if the left index is greater than or equal to the right index. The loop changes the iteration helper variables according to the relation between the two current values of the list. The actual width is the difference between the right and the left indices. The actual height is the current minimum value of the bounded list elements. The actual area is the product of the actual height and actual width. If this value is greater than the previous one, then the variable will be updated with this new value.
</p>
</section>
<br>
<section id="leetcode11.2" class="leetcode-sulotions-container">
<h2>LeetCode 11.2.</h2>
<h3><a href="https://leetcode.com/problems/container-with-most-water/description/" target="_blank" title="LeetCode Link">Container With Most Water</a> 🥛</h3>
<h3>Programming language: <a href="https://go.dev/" target="_blank" title="Golang lang Link">Golang</a> 🐹</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_11_no1_2" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
I used a two-pointer based solution, so I iterate through the given list from the left and the right sides. I implemented an infinite loop that breaks if the left index is greater than or equal to the right index. The loop changes the iteration helper variables according to the relation between the two current values of the list. The actual width is the difference between the right and the left indices. The actual height is the current minimum value of the bounded list elements. The actual area is the product of the actual height and actual width. If this value is greater than the previous one, then the variable will be updated with this new value.
</p>
</section>
<br>
<section id="leetcode12" class="leetcode-sulotions-container">
<h2>LeetCode 12.</h2>
<h3><a href="https://leetcode.com/problems/integer-to-roman/description/" target="_blank" title="LeetCode Link">Integer to Roman</a> 🏛️</h3>
<h3>Programming language: <a href="https://rust-lang.org/" target="_blank" title="Rust lang Link">Rust</a> 🦀</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_12_no1" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First of all, I created a loop that iterates backward. I defined a place variable using the iterator and the length of the string (which is basically a standard index, but calculated from the end). This variable provides the place value (ones, tens, etc.) for a match expression that evaluates every possibility. For example, the last index in the loop (which represents the ones place, or 0 in this case) triggers the 'ones' branch. This branch checks the character at the current index and returns the corresponding Roman numeral string, which is then prepended to the result container.
</p>
</section>
<br>
<section id="leetcode12.2" class="leetcode-sulotions-container">
<h2>LeetCode 12.2.</h2>
<h3><a href="https://leetcode.com/problems/integer-to-roman/description/" target="_blank" title="LeetCode Link">Integer to Roman</a> 🏛️</h3>
<h3>Programming language: <a href="https://go.dev/" target="_blank" title="Golang lang Link">Golang</a> 🐹</h3>
<h3><a href="https://github.com/6gDav/LeetCode_problem_12_no1_2" target="_blank">My solutions</a> ☺️</h3>
<hr>
<h3>🧠 How I solved the problem</h3>
<p>
First of all, I created a loop that iterates backward. I defined a place variable using the iterator and the length of the string (which is basically a standard index, but calculated from the end). This variable provides the place value (ones, tens, etc.) for a match expression that evaluates every possibility. For example, the last index in the loop (which represents the ones place, or 0 in this case) triggers the 'ones' branch. This branch checks the character at the current index and returns the corresponding Roman numeral string, which is then prepended to the result container.
</p>
</section>
<br>
</div></section></main>
</body></html>