Skip to content

Commit 591fc3d

Browse files
committed
Prepare for publication
1 parent 94dab56 commit 591fc3d

Some content is hidden

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

65 files changed

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

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

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

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ int uniquePaths(final int _rows, final int _cols) {
1515

1616
for (int r = 1; r < _rows; r++) {
1717
for (int c = 1; c < _cols; c++) {
18-
// printf("%d/%d%n", r, c);
1918
dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
2019
}
2120
}

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

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Problem0077 extends LeetcodeProblem {
1616
* @return all possible combinations
1717
*/
1818
public List<List<Integer>> combine(int _n, int _k) {
19-
// printf("n=%d, k=%d%n", n, k);
2019
if (_k > _n || _k < 0) {
2120
return List.of();
2221
} else if (_k == 0) {
@@ -26,13 +25,11 @@ public List<List<Integer>> combine(int _n, int _k) {
2625
}
2726

2827
List<List<Integer>> res = combine(_n - 1, _k - 1);
29-
// printf("res 1=%s%n", res);
3028
// if at this point res is empty, it can only be that k-1 == 0,
3129
// n-1 < k-1 is not possible since n>=k (if n<k, the function would have already returned at an early point)
3230
for (List<Integer> list : res) {
3331
list.add(_n);
3432
}
35-
// printf("res 2=%s%n", res);
3633
res.addAll(combine(_n - 1, _k));
3734
return res;
3835
}

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

-11
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,4 @@ static String scramble(ThreadLocalRandom _rand, String _str) {
140140
}
141141
}
142142

143-
public static void main(String[] _args) {
144-
String str = "great";
145-
int attempts = 10_000;
146-
Set<String> results = new HashSet<>();
147-
// IntStream.range(0, attempts).forEach(i -> System.out.printf("Scramble %s -> %s %n", str, scramble(str)));
148-
ThreadLocalRandom rand = ThreadLocalRandom.current();
149-
IntStream.range(0, attempts).forEach(i -> results.add(scramble(rand, str)));
150-
System.out.printf("%d results:%n%s%n", results.size(), String.join("\n", results));
151-
// results.stream().forEach(System.out::println);
152-
}
153-
154143
}

src/main/java/io/github/spannm/leetcode/lc0/lc0100/Problem0131.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7-
import java.util.stream.Collectors;
87

98
/**
109
* <a href="https://leetcode.com/problems/palindrome-partitioning/">131. Palindrome Partitioning</a>.
1110
*/
1211
class Problem0131 extends LeetcodeProblem {
1312

1413
List<List<String>> partition(String _str) {
15-
List<List<String>> result = dfs(0, new ArrayList<>(), new ArrayList<>(), _str);
16-
printf("\"%s; %s,\"%n", _str, result.stream()
17-
.map(List::stream)
18-
.map(s -> '[' + s.collect(Collectors.joining(",")) + ']')
19-
.collect(Collectors.joining(", ")));
20-
return result;
14+
return dfs(0, new ArrayList<>(), new ArrayList<>(), _str);
2115
}
2216

2317
static List<List<String>> dfs(int _start, List<List<String>> _result, List<String> _currentList, String _str) {

src/main/java/io/github/spannm/leetcode/lc0/lc0100/Problem0136.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ int singleNumber2(int[] _nums) {
2626
return _nums[len - 1];
2727
}
2828

29-
public int singleNumber(final int[] _nums) {
30-
// printf("Input: %s %n", Arrays.toString(_nums));
31-
final int len = _nums.length;
29+
int singleNumber(int[] _nums) {
30+
int len = _nums.length;
3231
if (len == 1) {
3332
return _nums[0];
3433
}
3534

3635
int n = _nums[0];
3736
for (int i = 1; i < len; i++) {
3837
n = n ^ _nums[i]; // bitwise exclusive OR
39-
// printf("%d -> %d %n", _nums[i], n);
4038
}
4139
return n;
4240
}

src/main/java/io/github/spannm/leetcode/lc0/lc0100/Problem0137.java

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ int singleNumber2(final int[] _nums) {
1616
}
1717

1818
Arrays.sort(_nums);
19-
printf("Input: %s %n", Arrays.toString(_nums));
2019

2120
for (int i = 1; i < len; i += 3) {
2221
if (_nums[i] - _nums[i - 1] != 0) {

src/main/java/io/github/spannm/leetcode/lc0/lc0100/Problem0142.java

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

1111
ListNode detectCycle(ListNode _head) {
12-
printf("Head: %s%n", _head);
1312
ListNode fast = _head;
1413
ListNode slow = _head;
1514

0 commit comments

Comments
 (0)