Skip to content

Commit 51dbbca

Browse files
committed
Prepare for publication
1 parent 94dab56 commit 51dbbca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+100
-438
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
.~lock*
1515
*.kate-swp
1616
**/Migrate*.java
17+
**/RenameMethodParms.java

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023- Markus S.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Java Solutions to Leetcode Problems
2+
3+
In this repo you will find a collection of 2500+ Java solutions to Leetcode coding problems and unit tests.
4+
5+
I participated in Leetcode for quite some time, <br>
6+
peaking in a 500-day streak of solving the daily challenge, <br>
7+
even earning a free T-Shirt (which turned out to kind of small and could not be exchanged for a larger one unless shipped back to China :)
8+
9+
<figure>
10+
<img src="src/test/resources/img/my-leetcode-streak.jpeg" alt="500-Day Streak">
11+
</figure>
12+
13+
Most solutions have been successfully submitted. <br>
14+
Many come with JUnit tests using test data found in the problem descriptions.
15+
16+
This project requires Maven and Java 17.<br>
17+
18+
It has neither compile nor run-time, only test dependencies.<br>
19+
Unit tests are based on JUnit 5 and @ParameterizedTest.<br>
20+
The code is structured as a typical standard Maven project and fairly self-explanatory.<br>
21+
22+
Solutions are named *ProblemNNNN*, tests are named *ProblemNNNNTest*,<br>
23+
where *NNNN* is the problem number formatted to four digits.<br>
24+
25+
To build the project simply type: `mvn` (defaults to `mvn clean verify`)<br>
26+
<br>
27+
28+
- Use as you wish and as you feel fit.<br>
29+
30+
- No warranty given.<br>
31+
32+
- No need to give credit.<br>
33+
34+
Hope you will find it useful.
35+
36+
### Happy coding!
37+
38+
<figure>
39+
<img src="src/test/resources/img/my-leetcode-tee.jpeg" alt="Free Tee">
40+
<figcaption>My free T-Shirt (says 'Large' but fits like 'Medium')</figcaption>
41+
</figure>

pom.xml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
<packaging>jar</packaging>
1515

1616
<name>${project.artifactId}</name>
17-
<description>Solutions to Leetcode coding problems.</description>
17+
<description>2500+ solutions to Leetcode coding problems</description>
1818
<url>https://github.com/${developerId}/${project.artifactId}</url>
1919
<inceptionYear>2023</inceptionYear>
2020

21+
<licenses>
22+
<license>
23+
<name>MIT License</name>
24+
<url>${project.url}/blob/master/LICENSE</url>
25+
</license>
26+
</licenses>
27+
2128
<developers>
2229
<developer>
2330
<id>${developerId}</id>
24-
<name>Markus Spann</name>
31+
<name>Markus S.</name>
2532
<email>[email protected]</email>
2633
<organizationUrl>https://github.com/${developerId}/</organizationUrl>
27-
<timezone>+1</timezone>
2834
</developer>
2935
</developers>
3036

@@ -121,6 +127,7 @@
121127
<defaultGoal>clean verify</defaultGoal>
122128

123129
<pluginManagement>
130+
124131
<plugins>
125132

126133
<plugin>
@@ -154,8 +161,6 @@
154161
</plugins>
155162
</pluginManagement>
156163

157-
<plugins/>
158-
159164
</build>
160165

161166
<profiles>
@@ -168,7 +173,7 @@
168173
<properties>
169174
<!-- skip _compiling_ the tests -->
170175
<maven.test.skip>true</maven.test.skip>
171-
<!-- skip the tests -->
176+
<!-- skip test execution -->
172177
<skipTests>true</skipTests>
173178

174179
<maven.javadoc.skip>true</maven.javadoc.skip>

src/main/java/io/github/spannm/leetcode/LeetcodeProblem.java

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,9 @@
22

33
public abstract class LeetcodeProblem {
44

5-
private final boolean doOutput;
6-
7-
protected LeetcodeProblem() {
8-
this(true);
9-
}
10-
11-
protected LeetcodeProblem(boolean _doOutput) {
12-
doOutput = _doOutput;
13-
}
14-
15-
public final boolean doOutput() {
16-
return doOutput;
17-
}
18-
19-
public final void printf(String _format, Object... _args) {
20-
if (doOutput) {
21-
System.out.printf(_format, _args);
22-
}
23-
}
24-
25-
public final void println(Object _arg) {
26-
if (doOutput) {
27-
System.out.println(_arg);
28-
}
29-
}
30-
315
@Override
326
public String toString() {
33-
return getClass().getSimpleName() + "[]";
7+
return String.format("%s[]", getClass().getSimpleName());
348
}
359

3610
public static String asString(Object _o) {

src/main/java/io/github/spannm/leetcode/LeetcodeSqlProblem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ public abstract class LeetcodeSqlProblem extends LeetcodeProblem {
55
private final String sql;
66

77
protected LeetcodeSqlProblem(String _sql) {
8-
super(true);
98
sql = _sql;
109
}
1110

src/main/java/io/github/spannm/leetcode/dep/Sudoku.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ public boolean solve(Board _board) {
3737

3838
List<Field> fields = _board.boxes.stream().flatMap(b -> b.getFields().stream()).toList();
3939

40-
// inspect field by field
41-
for (Field currFld : fields) {
42-
// if (currFld.getRowNo() == 'A' && currFld.getColNo() == 1) {
43-
// doOutput(" DEBUG ");
44-
// }
45-
// doOutput(" %s%n", currFld);
40+
for (Field currFld : fields) { // inspect field by field
4641

4742
// check all remaining possible values whether one is only possible in this field
4843
for (Area area : currFld.getAreas()) {

src/main/java/io/github/spannm/leetcode/lc0/lc0000/Problem0001.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@
22

33
import io.github.spannm.leetcode.LeetcodeProblem;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
/**
69
* <a href="https://leetcode.com/problems/two-sum/">1. Two Sum</a>.
710
*/
811
class Problem0001 extends LeetcodeProblem {
912

1013
int[] twoSum(int[] _nums, int _target) {
11-
if (doOutput()) {
12-
printf("Input: %s, target: %d%n", java.util.Arrays.toString(_nums), _target);
13-
}
14-
return impl1(_nums, _target);
15-
}
16-
17-
int[] impl1(int[] _nums, int _target) {
18-
final java.util.List<IdxAndNum> idxAndNums = new java.util.ArrayList<>();
14+
List<IdxAndNum> idxAndNums = new ArrayList<>();
1915
for (int i = 0; i < _nums.length; i++) {
2016
idxAndNums.add(new IdxAndNum(i, _nums[i]));
2117
}
2218
idxAndNums.sort(java.util.Comparator.comparing(IdxAndNum::getNum).thenComparing(IdxAndNum::getIdx));
2319

2420
final int len = idxAndNums.size();
2521

26-
if (doOutput()) {
27-
printf("Values (%s): %s%n", len, idxAndNums);
28-
}
29-
3022
int[] result = null;
3123

3224
for (int i1 = 0; i1 < len - 1; i1++) {
@@ -40,19 +32,14 @@ int[] impl1(int[] _nums, int _target) {
4032

4133
}
4234

43-
System.err.printf("No result found! %n%n");
4435
return null;
4536
}
4637

4738
/**
4839
* Calculates the sum of two values.<br>
4940
* If equal to {@code target}, returns their indices in an int array.
5041
*/
51-
int[] calcSum(IdxAndNum _in1, IdxAndNum _in2, int _target) {
52-
if (doOutput()) {
53-
printf(" Test %s + %s = %s %n%n", _in1, _in2, _target);
54-
}
55-
42+
static int[] calcSum(IdxAndNum _in1, IdxAndNum _in2, int _target) {
5643
if (_in1.getNum() + _in2.getNum() == _target) {
5744
return new int[] {_in1.getIdx(), _in2.getIdx()};
5845
}

src/main/java/io/github/spannm/leetcode/lc0/lc0000/Problem0004.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
class Problem0004 extends LeetcodeProblem {
1414

1515
double findMedianSortedArrays(int[] _nums1, int[] _nums2) {
16-
final LinkedList<Integer> l1 = Arrays.stream(_nums1).boxed().collect(Collectors.toCollection(LinkedList::new));
17-
final LinkedList<Integer> l2 = Arrays.stream(_nums2).boxed().collect(Collectors.toCollection(LinkedList::new));
16+
LinkedList<Integer> l1 = Arrays.stream(_nums1).boxed().collect(Collectors.toCollection(LinkedList::new));
17+
LinkedList<Integer> l2 = Arrays.stream(_nums2).boxed().collect(Collectors.toCollection(LinkedList::new));
1818

1919
// merge sorted arrays into one
2020
final LinkedList<Integer> merged = new LinkedList<>();
21-
// Solange noch nicht beide Listen hinzugefügt worden
2221
while (!l1.isEmpty() && !l2.isEmpty()) {
2322
if (l1.getFirst() <= l2.getFirst()) {
2423
merged.addLast(l1.removeFirst());
2524
} else {
2625
merged.addLast(l2.removeFirst());
2726
}
2827
}
29-
// Füge Rest hinzu, falls etwas übrig ist.
3028
merged.addAll(l1);
3129
merged.addAll(l2);
3230

@@ -46,15 +44,9 @@ List<Integer> merge(LinkedList<Integer> _l1, LinkedList<Integer> _l2) {
4644
merged.addLast(_l2.removeFirst());
4745
}
4846
}
49-
// Füge Rest hinzu, falls etwas übrig ist.
5047
merged.addAll(_l1);
5148
merged.addAll(_l2);
5249
return merged;
5350
}
5451

55-
public void printFindMedianSortedArrays(int[] _nums1, int[] _nums2) {
56-
double result = findMedianSortedArrays(_nums1, _nums2);
57-
printf("%s and %s => %s%n", asString(_nums1), asString(_nums2), result);
58-
}
59-
6052
}

src/main/java/io/github/spannm/leetcode/lc0/lc0000/Problem0006.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ String convert(String _s, int _numRows) {
2323

2424
for (int i = 0; i < arr.length; i++) {
2525
sbs.get(z).append(arr[i]);
26-
printf("i=%s, z=%s, v=%s %s %n", i, z, arr[i], up ? "up" : "down");
2726
if (up) {
2827
z++;
2928
} else {
@@ -34,10 +33,6 @@ String convert(String _s, int _numRows) {
3433
}
3534
}
3635

37-
if (doOutput()) {
38-
sbs.forEach(this::println);
39-
}
40-
4136
return sbs.stream().map(StringBuilder::toString).collect(Collectors.joining(""));
4237
}
4338

0 commit comments

Comments
 (0)