Skip to content

Commit 885f7d1

Browse files
committed
Initial commit
0 parents  commit 885f7d1

File tree

3,163 files changed

+152978
-0
lines changed

Some content is hidden

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

3,163 files changed

+152978
-0
lines changed

.github/workflows/maven.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
3+
4+
# This workflow uses actions that are not certified by GitHub.
5+
# They are provided by a third-party and are governed by
6+
# separate terms of service, privacy policy, and support
7+
# documentation.
8+
9+
name: Java CI with Maven
10+
11+
on:
12+
push:
13+
branches: [ "master" ]
14+
pull_request:
15+
branches: [ "master" ]
16+
17+
jobs:
18+
build:
19+
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Set up JDK 11
25+
uses: actions/setup-java@v3
26+
with:
27+
java-version: '17'
28+
distribution: 'temurin'
29+
cache: maven
30+
- name: Build with Maven
31+
run: mvn -B clean verify --file pom.xml
32+
33+
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
34+
- name: Update dependency graph
35+
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**/.classpath
2+
**/.project
3+
**/.settings
4+
**/bin/
5+
**/target/
6+
**/.eclipse-pmd
7+
**/.pmd
8+
**/.pmdruleset.xml
9+
**/NOTES.txt
10+
*.iml
11+
.checkstyle
12+
.idea/
13+
.project
14+
.~lock*
15+
*.kate-swp
16+
**/internal/*
17+

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

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Java Solutions to LeetCode Problems
2+
3+
In this repo you will find a collection of hundreds of Java solutions to [LeetCode coding problems](https://leetcode.com/problemset/).
4+
5+
I was active on LeetCode for quite some time,<br>
6+
peaking in a 500-day streak of solving the daily challenge,<br>
7+
and even earning a free T-Shirt (which turned out too small and could not be exchanged :)
8+
9+
<figure>
10+
<img src="src/test/resources/img/my-leetcode-streak.jpeg" alt="500-Day Streak">
11+
</figure>
12+
13+
This project requires Maven and Java 17.<br>
14+
15+
It has neither compile nor run-time, only test dependencies.<br>
16+
Unit tests are based on JUnit 5 and *@ParameterizedTest*.<br>
17+
18+
Most solutions have been successfully submitted.<br>
19+
Many come with JUnit tests using test data found in the problem descriptions.<br>
20+
21+
Solutions are named *ProblemNNNN*, tests are named *ProblemNNNNTest*,<br>
22+
where *NNNN* is the problem number formatted to four digits with leading zeroes.<br>
23+
24+
To build the project simply type: `mvn` (defaults to `mvn clean verify`)<br>
25+
26+
- Use as you wish and as you see fit.<br>
27+
28+
- No warranty given.<br>
29+
30+
- No need to give credit.<br>
31+
32+
- Hope you'll find it useful.<br>
33+
34+
### Happy coding!
35+
36+
<figure>
37+
<img src="src/test/resources/img/my-leetcode-tee.jpeg" alt="Free Tee">
38+
<figcaption>Free Tee</figcaption>
39+
</figure>

pom.xml

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.basepom</groupId>
7+
<artifactId>basepom-oss</artifactId>
8+
<version>59</version>
9+
</parent>
10+
11+
<groupId>io.github.spannm</groupId>
12+
<artifactId>leetcode</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
16+
<name>${project.artifactId}</name>
17+
<description>Solutions to LeetCode coding problems</description>
18+
<url>https://github.com/${developerId}/${project.artifactId}</url>
19+
<inceptionYear>2023</inceptionYear>
20+
21+
<licenses>
22+
<license>
23+
<name>MIT License</name>
24+
<url>${project.url}/blob/master/LICENSE</url>
25+
</license>
26+
</licenses>
27+
28+
<developers>
29+
<developer>
30+
<id>${developerId}</id>
31+
<name>Markus S.</name>
32+
<email>[email protected]</email>
33+
<organizationUrl>https://github.com/${developerId}/</organizationUrl>
34+
</developer>
35+
</developers>
36+
37+
<properties>
38+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39+
40+
<project.build.systemJdk>17</project.build.systemJdk>
41+
<project.build.targetJdk>17</project.build.targetJdk>
42+
43+
<developerId>spannm</developerId>
44+
45+
<dep.plugin.sortpom.version>4.0.0</dep.plugin.sortpom.version>
46+
47+
<dep.javapoet.version>1.13.0</dep.javapoet.version>
48+
<dep.slf4j-api.version>2.1.0-alpha1</dep.slf4j-api.version>
49+
<dep.junit.version>5.10.2</dep.junit.version>
50+
<dep.assertj.version>3.26.0</dep.assertj.version>
51+
52+
<basepom.check.fail-dependency>false</basepom.check.fail-dependency>
53+
<basepom.check.skip-spotbugs>true</basepom.check.skip-spotbugs>
54+
<basepom.check.skip-license>true</basepom.check.skip-license>
55+
</properties>
56+
57+
<dependencyManagement>
58+
<dependencies>
59+
<dependency>
60+
<groupId>org.junit</groupId>
61+
<artifactId>junit-bom</artifactId>
62+
<version>${dep.junit.version}</version>
63+
<type>pom</type>
64+
<scope>import</scope>
65+
</dependency>
66+
</dependencies>
67+
</dependencyManagement>
68+
69+
<dependencies>
70+
71+
<dependency>
72+
<groupId>com.squareup</groupId>
73+
<artifactId>javapoet</artifactId>
74+
<version>${dep.javapoet.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.slf4j</groupId>
80+
<artifactId>slf4j-api</artifactId>
81+
<version>${dep.slf4j-api.version}</version>
82+
<scope>test</scope>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.slf4j</groupId>
87+
<artifactId>slf4j-simple</artifactId>
88+
<version>${dep.slf4j-api.version}</version>
89+
<scope>test</scope>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>org.junit.jupiter</groupId>
94+
<artifactId>junit-jupiter-api</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>org.junit.jupiter</groupId>
100+
<artifactId>junit-jupiter-params</artifactId>
101+
<scope>test</scope>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>org.junit.platform</groupId>
106+
<artifactId>junit-platform-launcher</artifactId>
107+
<scope>test</scope>
108+
</dependency>
109+
110+
<dependency>
111+
<groupId>org.junit.platform</groupId>
112+
<artifactId>junit-platform-engine</artifactId>
113+
<scope>test</scope>
114+
</dependency>
115+
116+
<dependency>
117+
<groupId>org.assertj</groupId>
118+
<artifactId>assertj-core</artifactId>
119+
<version>${dep.assertj.version}</version>
120+
<scope>test</scope>
121+
</dependency>
122+
123+
</dependencies>
124+
125+
<build>
126+
127+
<defaultGoal>clean verify</defaultGoal>
128+
129+
<pluginManagement>
130+
131+
<plugins>
132+
133+
<plugin>
134+
<artifactId>maven-checkstyle-plugin</artifactId>
135+
<configuration>
136+
<configLocation>src/test/resources/team/checkstyle-rules.xml</configLocation>
137+
</configuration>
138+
</plugin>
139+
140+
<plugin>
141+
<groupId>org.apache.maven.plugins</groupId>
142+
<artifactId>maven-pmd-plugin</artifactId>
143+
<configuration>
144+
<rulesets>
145+
<ruleset>src/test/resources/team/pmd-rules.xml</ruleset>
146+
</rulesets>
147+
</configuration>
148+
</plugin>
149+
150+
<plugin>
151+
<groupId>com.github.ekryd.sortpom</groupId>
152+
<artifactId>sortpom-maven-plugin</artifactId>
153+
<version>${dep.plugin.sortpom.version}</version>
154+
<configuration>
155+
<createBackupFile>false</createBackupFile>
156+
<expandEmptyElements>false</expandEmptyElements>
157+
<nrOfIndentSpace>4</nrOfIndentSpace>
158+
</configuration>
159+
</plugin>
160+
161+
</plugins>
162+
</pluginManagement>
163+
164+
</build>
165+
166+
<profiles>
167+
<profile>
168+
<id>fast</id>
169+
<!-- Profile to skip time-consuming steps. -->
170+
<activation>
171+
<activeByDefault>false</activeByDefault>
172+
</activation>
173+
<properties>
174+
<!-- skip _compiling_ the tests -->
175+
<maven.test.skip>true</maven.test.skip>
176+
<!-- skip test execution -->
177+
<skipTests>true</skipTests>
178+
179+
<maven.javadoc.skip>true</maven.javadoc.skip>
180+
<basepom.check.skip-extended>true</basepom.check.skip-extended>
181+
</properties>
182+
</profile>
183+
</profiles>
184+
185+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.spannm.leetcode;
2+
3+
public abstract class LeetcodeProblem {
4+
5+
@Override
6+
public String toString() {
7+
return String.format("%s[]", getClass().getSimpleName());
8+
}
9+
10+
public static String asString(Object _o) {
11+
if (_o == null) {
12+
return "null";
13+
} else if (_o.getClass().isArray()) {
14+
final int len = java.lang.reflect.Array.getLength(_o);
15+
Object[] arr = new Object[len];
16+
for (int i = 0; i < len; i++) {
17+
arr[i] = java.lang.reflect.Array.get(_o, i);
18+
}
19+
return "["
20+
+ java.util.Arrays.stream(arr)
21+
.map(LeetcodeProblem::asString)
22+
.collect(java.util.stream.Collectors.joining(","))
23+
+ "]";
24+
}
25+
return _o.toString();
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.spannm.leetcode;
2+
3+
/**
4+
* Unspecific {@code Leetcode} run-time exception.
5+
*/
6+
public final class LeetcodeRuntimeException extends RuntimeException {
7+
8+
private static final long serialVersionUID = 1L;
9+
10+
public LeetcodeRuntimeException(String _message) {
11+
this(_message, null);
12+
}
13+
14+
public LeetcodeRuntimeException(Throwable _cause) {
15+
this(null, _cause);
16+
}
17+
18+
public LeetcodeRuntimeException(String _message, Throwable _cause) {
19+
super(_message, _cause);
20+
}
21+
22+
public static LeetcodeRuntimeException from(Throwable _ex) {
23+
return _ex instanceof LeetcodeRuntimeException ? (LeetcodeRuntimeException) _ex : new LeetcodeRuntimeException(_ex);
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.spannm.leetcode;
2+
3+
public abstract class LeetcodeSqlProblem extends LeetcodeProblem {
4+
5+
private final String sql;
6+
7+
protected LeetcodeSqlProblem(String _sql) {
8+
sql = _sql;
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return String.format("%s[sql=%s]", getClass().getSimpleName(), sql);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.spannm.leetcode.dep;
2+
3+
import java.util.List;
4+
5+
@SuppressWarnings({"PMD.ClassNamingConventions", "checkstyle:TypeNameCheck"})
6+
public interface BinaryMatrix {
7+
int get(int _row, int _col);
8+
9+
List<Integer> dimensions();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.spannm.leetcode.dep;
2+
3+
@SuppressWarnings({"PMD.ClassNamingConventions", "checkstyle:TypeNameCheck"})
4+
public interface GridMaster {
5+
boolean canMove(char _d);
6+
7+
int move(char _d);
8+
9+
boolean isTarget();
10+
}

0 commit comments

Comments
 (0)