Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Jul 4, 2024
0 parents commit 885f7d1
Show file tree
Hide file tree
Showing 3,163 changed files with 152,978 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
35 changes: 35 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B clean verify --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
**/.classpath
**/.project
**/.settings
**/bin/
**/target/
**/.eclipse-pmd
**/.pmd
**/.pmdruleset.xml
**/NOTES.txt
*.iml
.checkstyle
.idea/
.project
.~lock*
*.kate-swp
**/internal/*

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023- Markus S.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Java Solutions to LeetCode Problems

In this repo you will find a collection of hundreds of Java solutions to [LeetCode coding problems](https://leetcode.com/problemset/).

I was active on LeetCode for quite some time,<br>
peaking in a 500-day streak of solving the daily challenge,<br>
and even earning a free T-Shirt (which turned out too small and could not be exchanged :)

<figure>
<img src="src/test/resources/img/my-leetcode-streak.jpeg" alt="500-Day Streak">
</figure>

This project requires Maven and Java 17.<br>

It has neither compile nor run-time, only test dependencies.<br>
Unit tests are based on JUnit 5 and *@ParameterizedTest*.<br>

Most solutions have been successfully submitted.<br>
Many come with JUnit tests using test data found in the problem descriptions.<br>

Solutions are named *ProblemNNNN*, tests are named *ProblemNNNNTest*,<br>
where *NNNN* is the problem number formatted to four digits with leading zeroes.<br>

To build the project simply type: `mvn` (defaults to `mvn clean verify`)<br>

- Use as you wish and as you see fit.<br>

- No warranty given.<br>

- No need to give credit.<br>

- Hope you'll find it useful.<br>

### Happy coding!

<figure>
<img src="src/test/resources/img/my-leetcode-tee.jpeg" alt="Free Tee">
<figcaption>Free Tee</figcaption>
</figure>
185 changes: 185 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.basepom</groupId>
<artifactId>basepom-oss</artifactId>
<version>59</version>
</parent>

<groupId>io.github.spannm</groupId>
<artifactId>leetcode</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
<description>Solutions to LeetCode coding problems</description>
<url>/~https://github.com/${developerId}/${project.artifactId}</url>
<inceptionYear>2023</inceptionYear>

<licenses>
<license>
<name>MIT License</name>
<url>${project.url}/blob/master/LICENSE</url>
</license>
</licenses>

<developers>
<developer>
<id>${developerId}</id>
<name>Markus S.</name>
<email>spannm@outlook.de</email>
<organizationUrl>/~https://github.com/${developerId}/</organizationUrl>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.build.systemJdk>17</project.build.systemJdk>
<project.build.targetJdk>17</project.build.targetJdk>

<developerId>spannm</developerId>

<dep.plugin.sortpom.version>4.0.0</dep.plugin.sortpom.version>

<dep.javapoet.version>1.13.0</dep.javapoet.version>
<dep.slf4j-api.version>2.1.0-alpha1</dep.slf4j-api.version>
<dep.junit.version>5.10.2</dep.junit.version>
<dep.assertj.version>3.26.0</dep.assertj.version>

<basepom.check.fail-dependency>false</basepom.check.fail-dependency>
<basepom.check.skip-spotbugs>true</basepom.check.skip-spotbugs>
<basepom.check.skip-license>true</basepom.check.skip-license>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${dep.junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>${dep.javapoet.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${dep.slf4j-api.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${dep.slf4j-api.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${dep.assertj.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>

<defaultGoal>clean verify</defaultGoal>

<pluginManagement>

<plugins>

<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>src/test/resources/team/checkstyle-rules.xml</configLocation>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<rulesets>
<ruleset>src/test/resources/team/pmd-rules.xml</ruleset>
</rulesets>
</configuration>
</plugin>

<plugin>
<groupId>com.github.ekryd.sortpom</groupId>
<artifactId>sortpom-maven-plugin</artifactId>
<version>${dep.plugin.sortpom.version}</version>
<configuration>
<createBackupFile>false</createBackupFile>
<expandEmptyElements>false</expandEmptyElements>
<nrOfIndentSpace>4</nrOfIndentSpace>
</configuration>
</plugin>

</plugins>
</pluginManagement>

</build>

<profiles>
<profile>
<id>fast</id>
<!-- Profile to skip time-consuming steps. -->
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<!-- skip _compiling_ the tests -->
<maven.test.skip>true</maven.test.skip>
<!-- skip test execution -->
<skipTests>true</skipTests>

<maven.javadoc.skip>true</maven.javadoc.skip>
<basepom.check.skip-extended>true</basepom.check.skip-extended>
</properties>
</profile>
</profiles>

</project>
28 changes: 28 additions & 0 deletions src/main/java/io/github/spannm/leetcode/LeetcodeProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.spannm.leetcode;

public abstract class LeetcodeProblem {

@Override
public String toString() {
return String.format("%s[]", getClass().getSimpleName());
}

public static String asString(Object _o) {
if (_o == null) {
return "null";
} else if (_o.getClass().isArray()) {
final int len = java.lang.reflect.Array.getLength(_o);
Object[] arr = new Object[len];
for (int i = 0; i < len; i++) {
arr[i] = java.lang.reflect.Array.get(_o, i);
}
return "["
+ java.util.Arrays.stream(arr)
.map(LeetcodeProblem::asString)
.collect(java.util.stream.Collectors.joining(","))
+ "]";
}
return _o.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.spannm.leetcode;

/**
* Unspecific {@code Leetcode} run-time exception.
*/
public final class LeetcodeRuntimeException extends RuntimeException {

private static final long serialVersionUID = 1L;

public LeetcodeRuntimeException(String _message) {
this(_message, null);
}

public LeetcodeRuntimeException(Throwable _cause) {
this(null, _cause);
}

public LeetcodeRuntimeException(String _message, Throwable _cause) {
super(_message, _cause);
}

public static LeetcodeRuntimeException from(Throwable _ex) {
return _ex instanceof LeetcodeRuntimeException ? (LeetcodeRuntimeException) _ex : new LeetcodeRuntimeException(_ex);
}

}
16 changes: 16 additions & 0 deletions src/main/java/io/github/spannm/leetcode/LeetcodeSqlProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.spannm.leetcode;

public abstract class LeetcodeSqlProblem extends LeetcodeProblem {

private final String sql;

protected LeetcodeSqlProblem(String _sql) {
sql = _sql;
}

@Override
public String toString() {
return String.format("%s[sql=%s]", getClass().getSimpleName(), sql);
}

}
10 changes: 10 additions & 0 deletions src/main/java/io/github/spannm/leetcode/dep/BinaryMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.spannm.leetcode.dep;

import java.util.List;

@SuppressWarnings({"PMD.ClassNamingConventions", "checkstyle:TypeNameCheck"})
public interface BinaryMatrix {
int get(int _row, int _col);

List<Integer> dimensions();
}
10 changes: 10 additions & 0 deletions src/main/java/io/github/spannm/leetcode/dep/GridMaster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.spannm.leetcode.dep;

@SuppressWarnings({"PMD.ClassNamingConventions", "checkstyle:TypeNameCheck"})
public interface GridMaster {
boolean canMove(char _d);

int move(char _d);

boolean isTarget();
}
Loading

0 comments on commit 885f7d1

Please sign in to comment.