Skip to content

Commit ffe16bf

Browse files
committed
Prepare for publication
1 parent d9714b6 commit ffe16bf

File tree

354 files changed

+522
-2175
lines changed

Some content is hidden

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

354 files changed

+522
-2175
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ static final class IdxAndNum {
5555
num = _num;
5656
}
5757

58-
public int getIdx() {
58+
int getIdx() {
5959
return idx;
6060
}
6161

62-
public int getNum() {
62+
int getNum() {
6363
return num;
6464
}
6565

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

+4-25
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,19 @@
88
class Problem0011 extends LeetcodeProblem {
99

1010
int maxArea(final int[] _height) {
11-
return maxAreaOptimized(_height);
12-
}
13-
14-
public int maxAreaNaive(final int[] _height) {
15-
int minHeight = 0;
16-
int area = 0;
17-
int maxArea = 0;
18-
19-
for (int x1 = _height.length - 1; x1 >= 0; x1--) {
20-
for (int x2 = 0; x2 < x1; x2++) {
21-
minHeight = Math.min(_height[x1], _height[x2]);
22-
area = minHeight * (x1 - x2);
23-
if (area > maxArea) {
24-
maxArea = area;
25-
}
26-
}
27-
}
28-
return maxArea;
29-
}
30-
31-
public int maxAreaOptimized(final int[] _height) {
3211
int idxL = 0;
3312
int idxR = _height.length - 1;
3413
int hL = 0;
3514
int hR = 0;
3615
int area = 0;
37-
int maxArea = 0;
16+
int maxArea1 = 0;
3817

3918
while (idxL < idxR) {
4019
hL = _height[idxL];
4120
hR = _height[idxR];
4221
area = Math.min(hL, hR) * (idxR - idxL);
43-
if (area > maxArea) {
44-
maxArea = area;
22+
if (area > maxArea1) {
23+
maxArea1 = area;
4524
}
4625

4726
if (hL < hR) {
@@ -54,7 +33,7 @@ public int maxAreaOptimized(final int[] _height) {
5433
}
5534
}
5635
}
57-
return maxArea;
36+
return maxArea1;
5837
}
5938

6039
}

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

+3-70
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,14 @@
22

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

5-
import java.util.Map;
6-
import java.util.stream.Collectors;
7-
import java.util.stream.IntStream;
8-
95
/**
106
* <a href="https://leetcode.com/problems/integer-to-roman/">12. Integer to Roman</a>.
117
*/
128
class Problem0012 extends LeetcodeProblem {
139

14-
static final Map<Integer, String> MAP = IntStream.range(1, 4000).boxed().collect(Collectors.toMap(k -> k, Problem0012::intToRomanSwitch));
15-
16-
public String intToRoman(final int _num) {
17-
return intToRomanStaticMap(_num);
18-
}
19-
20-
static String intToRomanStaticMap(final int _num) {
21-
return MAP.get(_num);
22-
}
23-
24-
static String intToRomanSwitch(final int _num) {
25-
final int[] digits = {_num / 1000, _num / 100 % 10, _num / 10 % 10, _num % 10};
26-
final StringBuilder sb = new StringBuilder();
10+
String intToRoman(final int _num) {
11+
int[] digits = {_num / 1000, _num / 100 % 10, _num / 10 % 10, _num % 10};
12+
StringBuilder sb = new StringBuilder();
2713
for (int i = 0; i < digits.length; i++) {
2814
if (digits[i] < 1) {
2915
continue;
@@ -80,57 +66,4 @@ static String intToRomanSwitch(final int _num) {
8066
return sb.toString();
8167
}
8268

83-
static String intToRomanSubtraction(int _num) {
84-
final StringBuilder sb = new StringBuilder();
85-
while (_num >= 1000) {
86-
sb.append('M');
87-
_num -= 1000;
88-
}
89-
if (_num >= 900) {
90-
sb.append("CM");
91-
_num -= 900;
92-
} else if (_num >= 500) {
93-
sb.append('D');
94-
_num -= 500;
95-
}
96-
if (_num >= 400) {
97-
sb.append("CD");
98-
_num -= 400;
99-
} else {
100-
while (_num >= 100) {
101-
sb.append('C');
102-
_num -= 100;
103-
}
104-
}
105-
if (_num >= 90) {
106-
sb.append("XC");
107-
_num -= 90;
108-
} else if (_num >= 50) {
109-
sb.append('L');
110-
_num -= 50;
111-
} else if (_num >= 40) {
112-
sb.append("XL");
113-
_num -= 40;
114-
}
115-
while (_num >= 10) {
116-
sb.append('X');
117-
_num -= 10;
118-
}
119-
if (_num == 9) {
120-
sb.append("IX");
121-
return sb.toString();
122-
} else if (_num == 4) {
123-
sb.append("IV");
124-
return sb.toString();
125-
} else if (_num >= 5) {
126-
sb.append('V');
127-
_num -= 5;
128-
}
129-
while (_num >= 1) {
130-
sb.append('I');
131-
_num -= 1;
132-
}
133-
return sb.toString();
134-
}
135-
13669
}

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

+2-32
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010
class Problem0015 extends LeetcodeProblem {
1111

12-
List<List<Integer>> threeSum2(int[] _nums) {
13-
final int len = _nums.length;
12+
List<List<Integer>> threeSum(int[] _nums) {
13+
int len = _nums.length;
1414
if (len == 0) {
1515
return List.of();
1616
}
@@ -38,34 +38,4 @@ List<List<Integer>> threeSum2(int[] _nums) {
3838
return new ArrayList<>(set);
3939
}
4040

41-
public List<List<Integer>> threeSum(int[] _nums) {
42-
List<Integer> input = Arrays.stream(_nums).sorted().boxed().toList();
43-
final int len = input.size();
44-
if (len < 3) {
45-
return List.of();
46-
}
47-
48-
Set<List<Integer>> results = new HashSet<>();
49-
for (int first = 0; first < len - 2; first++) {
50-
int second = first + 1;
51-
int third = len - 1;
52-
int sum;
53-
while (second < third) {
54-
sum = input.get(first) + input.get(second) + input.get(third);
55-
if (sum == 0) {
56-
results.add(List.of(input.get(first), input.get(second), input.get(third)));
57-
second++;
58-
third--;
59-
} else if (sum < 0) {
60-
second++;
61-
} else {
62-
third--;
63-
}
64-
}
65-
66-
}
67-
68-
return new ArrayList<>(results);
69-
}
70-
7141
}

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

+5-47
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,25 @@
22

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

5-
import java.util.*;
6-
import java.util.stream.IntStream;
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.HashSet;
8+
import java.util.List;
79

810
/**
911
* <a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">17. Letter Combinations of a Phone
1012
* Number</a>.
1113
*/
1214
class Problem0017 extends LeetcodeProblem {
1315

14-
@SuppressWarnings("unchecked")
15-
private static final List<String>[] DIGITS_TO_LETTERS = new List[] {null, // 0
16-
null, // 1
17-
List.of("a", "b", "c"), // 2
18-
List.of("d", "e", "f"), // 3
19-
List.of("g", "h", "i"), List.of("j", "k", "l"), List.of("m", "n", "o"), List.of("p", "q", "r", "s"), List.of("t", "u", "v"), List.of("w", "x", "y", "z")
20-
};
21-
2216
private static final char[][] DIGITS_TO_CHARS = new char[][] {null, // 0
2317
null, // 1
2418
{'a', 'b', 'c'}, // 2
2519
{'d', 'e', 'f'}, // 3
2620
{'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}
2721
};
2822

29-
public List<String> letterCombinations(String _digits) {
23+
List<String> letterCombinations(String _digits) {
3024
if (_digits == null || _digits.isEmpty()) {
3125
return List.of();
3226
}
@@ -48,40 +42,4 @@ static Collection<String> cartesianProduct(String _input, StringBuilder _sb, int
4842
return _product;
4943
}
5044

51-
public List<String> letterCombinations2(String _digits) {
52-
final int len = _digits.length();
53-
54-
if (len == 0) {
55-
return List.of();
56-
}
57-
58-
List<List<String>> lol = IntStream.range(0, len)
59-
// .map(i -> digits.charAt(i) - '0')
60-
.map(i -> Character.getNumericValue(_digits.charAt(i)))
61-
.mapToObj(i -> DIGITS_TO_LETTERS[i])
62-
.toList();
63-
64-
if (len == 1) {
65-
return lol.get(0);
66-
}
67-
68-
Set<String> combinations = new LinkedHashSet<>();
69-
70-
generatePermutations(lol, combinations, 0, "");
71-
72-
return new ArrayList<>(combinations);
73-
}
74-
75-
// cartesian product
76-
void generatePermutations(List<List<String>> _lists, Collection<String> _result, int _depth, String _current) {
77-
if (_depth == _lists.size()) {
78-
_result.add(_current);
79-
return;
80-
}
81-
82-
for (String element : _lists.get(_depth)) {
83-
generatePermutations(_lists, _result, _depth + 1, _current + element);
84-
}
85-
}
86-
8745
}

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

+4-29
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,17 @@
22

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

5-
import java.util.*;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
68
import java.util.stream.LongStream;
79

810
/**
911
* <a href="https://leetcode.com/problems/4sum/">18. 4Sum</a>.
1012
*/
1113
class Problem0018 extends LeetcodeProblem {
1214

13-
List<List<Integer>> fourSum1(int[] _nums, int _target) {
14-
final int len = _nums.length;
15-
Arrays.sort(_nums);
16-
17-
Set<List<Integer>> sets = new HashSet<>();
18-
for (int i = 0; i < len - 3; i++) {
19-
for (int j = i + 1; j < len - 2; j++) {
20-
for (int k = j + 1; k < len - 1; k++) {
21-
for (int p = k + 1; p < len; p++) {
22-
int sum = _nums[i] + _nums[j] + _nums[k] + _nums[p];
23-
if (sum == _target) {
24-
sets.add(List.of(_nums[i], _nums[j], _nums[k], _nums[p]));
25-
}
26-
}
27-
}
28-
}
29-
}
30-
31-
List<List<Integer>> results = new ArrayList<>(sets);
32-
33-
_nums = null;
34-
sets = null;
35-
36-
return results;
37-
}
38-
39-
public List<List<Integer>> fourSum(int[] _nums, final int _target) {
15+
List<List<Integer>> fourSum(int[] _nums, final int _target) {
4016
final int len = _nums.length;
4117
if (len <= 4) {
4218
if (len == 4 && Arrays.stream(_nums).mapToLong(i -> i).sum() == _target) {
@@ -88,7 +64,6 @@ public List<List<Integer>> fourSum(int[] _nums, final int _target) {
8864
}
8965
}
9066
}
91-
nums = null;
9267
return results;
9368
}
9469

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

-37
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,4 @@ boolean isValid(String _s) {
2828
return stack.isEmpty();
2929
}
3030

31-
public boolean isValid2(String _s) {
32-
if (_s.length() % 2 != 0) {
33-
return false;
34-
}
35-
36-
final char[] charArray = _s.toCharArray();
37-
final char[] stack = new char[charArray.length];
38-
int head = 0;
39-
for (char c : charArray) {
40-
switch (c) {
41-
case '{':
42-
case '[':
43-
case '(':
44-
stack[head++] = c;
45-
continue;
46-
case '}':
47-
if (head == 0 || stack[--head] != '{') {
48-
return false;
49-
}
50-
break;
51-
case ')':
52-
if (head == 0 || stack[--head] != '(') {
53-
return false;
54-
}
55-
break;
56-
case ']':
57-
if (head == 0 || stack[--head] != '[') {
58-
return false;
59-
}
60-
break;
61-
default:
62-
break;
63-
}
64-
}
65-
return head == 0;
66-
}
67-
6831
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
class Problem0024 extends LeetcodeProblem {
1010

11-
static ListNode swapPairs(ListNode _head) {
11+
ListNode swapPairs(ListNode _head) {
1212
if (_head == null || _head.next == null) {
1313
return _head;
1414
}

0 commit comments

Comments
 (0)