Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Feb 26, 2024
1 parent 0df1cef commit 330831f
Show file tree
Hide file tree
Showing 17 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public boolean isMatch(String _str, String _pattern) {
if (_pattern.charAt(i) != '*') {
break;
} else {
int length2 = slen;
match[length2][i] = true;
match[slen][i] = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ static TreeNode buildTreeRecurse(int[] _inOrder, int _inStart, int _inEnd,
final int leftSz = rootIndex - _inStart;
final int rightSz = _inEnd - rootIndex;

final TreeNode root = new TreeNode(rootVal,
return new TreeNode(rootVal,
buildTreeRecurse(_inOrder, _inStart, rootIndex - 1,
_postOrder, _postStart, _postStart + leftSz - 1),
buildTreeRecurse(_inOrder, rootIndex + 1, _inEnd,
_postOrder, _postEnd - rightSz, _postEnd - 1));

return root;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ public void push(int _x) {
}

public int pop() {
int removed = queue.remove();
return removed;
return queue.remove();
}

public int top() {
int peeked = queue.peek();
return peeked;
return queue.peek();
}

public boolean empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public ListNode addTwoNumbers(ListNode _ln1, ListNode _ln2) {
BigDecimal bg2 = ListNode.toBigDecimal(_ln2, false);
BigDecimal sum = bg1.add(bg2);

ListNode sn = ListNode.fromCharArray(new StringBuilder(sum.toString()).reverse().toString().toCharArray());
// System.out.println(ln1 + " + " + ln2 + " = " + sum + " / " + sn);
return sn;
return ListNode.fromCharArray(new StringBuilder(sum.toString()).reverse().toString().toCharArray());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class Problem0452 extends LeetcodeProblem {

public int findMinArrowShots(int[]... _points) {
List<List<Integer>> l = Arrays.stream(_points).map(a -> Arrays.stream(a).boxed().collect(Collectors.toList())).collect(Collectors.toList());
int nbArrows = findMinArrowShots(l);
return nbArrows;
return findMinArrowShots(l);
}

int findMinArrowShots(List<List<Integer>> _points) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public int minFallingPathSum(int[][] _arr) {
}
}
}
int minSum = java.util.Arrays.stream(dp[size - 1], 0, size).min().orElse(-1);
return minSum;
return java.util.Arrays.stream(dp[size - 1], 0, size).min().orElse(-1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public double minAreaFreeRect(int[][] _points) {
if ((x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0) {
int ww = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
int hh = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1);
ans = Math.min(ans, Math.sqrt(1L * ww * hh));
ans = Math.min(ans, Math.sqrt((long) ww * hh));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public int[] sumEvenAfterQueries(int[] _arr, int[][] _queries) {
}

private int computeEvenSum(int[] _arr) {
int sum = Arrays.stream(_arr).filter(num -> num % 2 == 0).sum();
return sum;
return Arrays.stream(_arr).filter(num -> num % 2 == 0).sum();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public List<Integer> addToArrayForm(int[] _num, int _k) {
public List<Integer> addToArrayForm1(int[] _num, int _k) {
String strNum = java.util.Arrays.stream(_num).mapToObj(i -> i + "").collect(java.util.stream.Collectors.joining());
java.math.BigInteger sum = new java.math.BigInteger(strNum).add(java.math.BigInteger.valueOf(_k));
List<Integer> list = java.util.Arrays.stream(sum.toString().split("")).map(Integer::parseInt).toList();
return list;
return java.util.Arrays.stream(sum.toString().split("")).map(Integer::parseInt).toList();
}

public List<Integer> addToArrayForm2(int[] _num, int _k) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public int[] smallestSufficientTeam(final String[] _reqSkills, final List<List<S

findTeam(reqSkillsMask, candidateSkills, 0, 0, new ArrayList<>(), mainTeam);

int[] smallestTeam = mainTeam.stream().mapToInt(i -> i).toArray();

return smallestTeam;
return mainTeam.stream().mapToInt(i -> i).toArray();
}

private static void findTeam(final int _reqSkillsMask, final int[] _candidatesSkills, final int _teamSkills,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public int julianDay(int _year, int _month, int _day) {
int a = (14 - _month) / 12;
int y = _year + 4800 - a;
int m = _month + 12 * a - 3;
int jdn = _day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
return jdn;
return _day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int numOfMinutes2(final int _count, int _headId, int[] _manager, int[] _t
return _times[0];
}

int sum = IntStream.range(0, _count)
return IntStream.range(0, _count)
.filter(id -> id != _headId)
.peek(i -> System.out.println("id: " + i))
.mapToObj(id -> _manager[id])
Expand All @@ -68,7 +68,6 @@ public int numOfMinutes2(final int _count, int _headId, int[] _manager, int[] _t
.map(id -> _times[id])
.mapToInt(i -> i)
.sum();
return sum;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public List<String> stringMatching(String[] _words) {
}
}
}
List<String> result = new ArrayList<>(set);
return result;
return new ArrayList<>(set);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static String getDay(String _day) {
}

private static String getMonth(String _month) {
String result = switch (_month) {
return switch (_month) {
case "Jan" -> "01";
case "Feb" -> "02";
case "Mar" -> "03";
Expand All @@ -33,7 +33,6 @@ private static String getMonth(String _month) {
case "Dec" -> "12";
default -> "";
};
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public List<Integer> twoOutOfThree(int[] _nums1, int[] _nums2, int[] _nums3) {
ans.add(element);
}
}
List<Integer> result = new ArrayList<>(ans);
return result;
return new ArrayList<>(ans);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public int[] successfulPairs(final int[] _spells, final int[] _potions, final lo
int successCount = cache.computeIfAbsent(spell,
i -> {
int minIndex = binSearch(spell, _potions, _success);
int result = potionCount - minIndex;
return result;
return potionCount - minIndex;
});

results[s] = successCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,11 @@ Method findTestMethod(Class<?> _clazz) {
.findFirst().orElseThrow(
() -> new LeetcodeRuntimeException("No non-static methods found in inner classes of " + _clazz));
}
Method method = Arrays.stream(methods)
return Arrays.stream(methods)
.filter(m -> !Modifier.isStatic(m.getModifiers()))
.sorted(Comparator.comparingInt(Method::getModifiers))
.findFirst().orElseThrow(
() -> new LeetcodeRuntimeException("No non-static method found in " + _clazz));
return method;
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 330831f

Please sign in to comment.