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

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
.~lock*
1515
*.kate-swp
1616
**/Migrate*.java
17+
**/RenameMethodParms.java

LICENSE

+21
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

+41
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

+11-6
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

+1-27
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

-1
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

+1-6
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

+5-18
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

+2-10
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

-5
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

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

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class Problem0008 extends LeetcodeProblem {
66

77
int myAtoi(String _s) {
8-
printf("Input: %s %n", _s);
98
if (_s == null || _s.isEmpty()) {
109
return 0;
1110
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public List<String> letterCombinations2(String _digits) {
6868
Set<String> combinations = new LinkedHashSet<>();
6969

7070
generatePermutations(lol, combinations, 0, "");
71-
printf("Combinations: %s %n", combinations);
7271

7372
return new ArrayList<>(combinations);
7473
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
class Problem0019 extends LeetcodeProblem {
1010

1111
ListNode removeNthFromEnd(ListNode _head, int _n) {
12-
printf("Input: %s, n=%d%n", _head, _n);
1312
ListNode node = _head;
1413

1514
int count = 1;
@@ -25,7 +24,6 @@ ListNode removeNthFromEnd(ListNode _head, int _n) {
2524
}
2625

2726
int position = count - _n + 1; // position to remove
28-
printf("Position of node to remove: %d (%d from end)%n", position, _n);
2927
if (position == 1) {
3028
_head = _head.next;
3129
return _head;

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

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ int searchInsert(int[] _nums, int _target) {
1212
}
1313

1414
int searchInsert1(int[] _nums, int _target, int _fromIndex, int _toIndex) {
15-
printf("%s, target %d, from %d to %d %n", java.util.Arrays.toString(_nums), _target, _fromIndex, _toIndex);
1615
if (_target < _nums[_fromIndex]) {
1716
return _fromIndex;
1817
} else if (_target > _nums[_toIndex]) {

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

-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ void solveSudoku(char[][] _board) {
1616
Sudoku.Board b = new Sudoku.Board(getClass().getSimpleName(), _board);
1717
solver.solve(b);
1818
b.updateBoardMatrix(_board);
19-
20-
printf("Board one-liner: %s%n", b.toString("", ""));
21-
22-
for (Sudoku.Field f : b.getEmptyFields()) {
23-
printf(" Solved: %s%n", f);
24-
}
25-
2619
}
2720

2821
}

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

-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
class Problem0057 extends LeetcodeProblem {
1313

1414
int[][] insert(int[][] _intervals, int[] _newInterval) {
15-
printf("intervals: %s, new: %s%n", asString(_intervals), asString(_newInterval));
1615

1716
int arrLen = _intervals == null ? 0 : _intervals.length;
1817
if (arrLen == 0) {
@@ -39,7 +38,6 @@ int[][] insert(int[][] _intervals, int[] _newInterval) {
3938
int[] mergeInterval = null;
4039
for (int i = 0; i < arrLen; i++) {
4140
int[] currInterval = _intervals[i];
42-
printf("Current: %s%n", asString(currInterval));
4341

4442
if (_newInterval[0] >= currInterval[0] && _newInterval[1] <= currInterval[1]) { // within
4543
return _intervals;
@@ -57,14 +55,12 @@ int[][] insert(int[][] _intervals, int[] _newInterval) {
5755
}
5856
} else if (mergeInterval != null) { // currently merging
5957
if (mergeInterval[1] < currInterval[0]) {
60-
printf("Merge done, end interval: %s%n", mergeInterval[1]);
6158
intervalList.add(mergeInterval);
6259
intervalList.addAll(List.of(_intervals).subList(i, arrLen));
6360
break;
6461
} else if (i == arrLen - 1) { // last
6562
mergeInterval[1] = Math.max(mergeInterval[1], currInterval[1]);
6663
intervalList.add(mergeInterval);
67-
printf("Merge done, end interval: %s%n", mergeInterval[1]);
6864
break;
6965
} else {
7066
mergeInterval[1] = Math.max(mergeInterval[1], currInterval[1]);
@@ -73,17 +69,14 @@ int[][] insert(int[][] _intervals, int[] _newInterval) {
7369
} else {
7470
mergeInterval = new int[] {Math.min(currInterval[0], _newInterval[0]), Math.max(currInterval[1], _newInterval[1])
7571
};
76-
printf("Merging, begin interval: %s%n", mergeInterval[0]);
7772
if (i == arrLen - 1) { // last
7873
intervalList.add(mergeInterval);
79-
printf("Merge done, end interval: %s%n", mergeInterval[1]);
8074
break;
8175
}
8276
}
8377

8478
}
8579
_intervals = intervalList.toArray(new int[0][]);
86-
printf("Returning %s%n", asString(_intervals));
8780
return _intervals;
8881
}
8982

0 commit comments

Comments
 (0)