Add automatic linter (#4214)

This commit is contained in:
acbin
2023-06-09 20:05:14 +08:00
committed by GitHub
parent 00282efd8b
commit 415a04ea7f
188 changed files with 661 additions and 1133 deletions

View File

@ -23,7 +23,6 @@ public class CheckBinaryTreeIsValidBST {
return false;
}
return (
isBSTUtil(node.left, min, node.data - 1) && isBSTUtil(node.right, node.data + 1, max));
return (isBSTUtil(node.left, min, node.data - 1) && isBSTUtil(node.right, node.data + 1, max));
}
}

View File

@ -48,12 +48,10 @@ public class CheckTreeIsSymmetric {
return false;
}
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left)
&& isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
}
private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
return leftSubtreeRoot == null || rightSubtreeRoot == null
|| leftSubtreeRoot.data != rightSubtreeRoot.data;
return leftSubtreeRoot == null || rightSubtreeRoot == null || leftSubtreeRoot.data != rightSubtreeRoot.data;
}
}

View File

@ -36,8 +36,7 @@ public class CreateBinaryTreeFromInorderPreorder {
return createTreeOptimized(preorder, inorderMap, 0, 0, inorder.length);
}
private static Node createTree(final Integer[] preorder, final Integer[] inorder,
final int preStart, final int inStart, final int size) {
private static Node createTree(final Integer[] preorder, final Integer[] inorder, final int preStart, final int inStart, final int size) {
if (size == 0) {
return null;
}
@ -50,14 +49,11 @@ public class CreateBinaryTreeFromInorderPreorder {
int leftNodesCount = i - inStart;
int rightNodesCount = size - leftNodesCount - 1;
root.left = createTree(preorder, inorder, preStart + 1, inStart, leftNodesCount);
root.right
= createTree(preorder, inorder, preStart + leftNodesCount + 1, i + 1, rightNodesCount);
root.right = createTree(preorder, inorder, preStart + leftNodesCount + 1, i + 1, rightNodesCount);
return root;
}
private static Node createTreeOptimized(final Integer[] preorder,
final Map<Integer, Integer> inorderMap, final int preStart, final int inStart,
final int size) {
private static Node createTreeOptimized(final Integer[] preorder, final Map<Integer, Integer> inorderMap, final int preStart, final int inStart, final int size) {
if (size == 0) {
return null;
}
@ -66,10 +62,8 @@ public class CreateBinaryTreeFromInorderPreorder {
int i = inorderMap.get(preorder[preStart]);
int leftNodesCount = i - inStart;
int rightNodesCount = size - leftNodesCount - 1;
root.left
= createTreeOptimized(preorder, inorderMap, preStart + 1, inStart, leftNodesCount);
root.right = createTreeOptimized(
preorder, inorderMap, preStart + leftNodesCount + 1, i + 1, rightNodesCount);
root.left = createTreeOptimized(preorder, inorderMap, preStart + 1, inStart, leftNodesCount);
root.right = createTreeOptimized(preorder, inorderMap, preStart + leftNodesCount + 1, i + 1, rightNodesCount);
return root;
}
}

View File

@ -37,8 +37,7 @@ public class KDTree {
if (points.length == 0) throw new IllegalArgumentException("Points array cannot be empty");
this.k = points[0].getDimension();
for (Point point : points)
if (point.getDimension() != k)
throw new IllegalArgumentException("Points must have the same dimension");
if (point.getDimension() != k) throw new IllegalArgumentException("Points must have the same dimension");
this.root = build(points, 0);
}
@ -49,13 +48,11 @@ public class KDTree {
*
*/
KDTree(int[][] pointsCoordinates) {
if (pointsCoordinates.length == 0)
throw new IllegalArgumentException("Points array cannot be empty");
if (pointsCoordinates.length == 0) throw new IllegalArgumentException("Points array cannot be empty");
this.k = pointsCoordinates[0].length;
Point[] points = Arrays.stream(pointsCoordinates).map(Point::new).toArray(Point[] ::new);
for (Point point : points)
if (point.getDimension() != k)
throw new IllegalArgumentException("Points must have the same dimension");
if (point.getDimension() != k) throw new IllegalArgumentException("Points must have the same dimension");
this.root = build(points, 0);
}
@ -224,8 +221,7 @@ public class KDTree {
*
*/
public void insert(Point point) {
if (point.getDimension() != k)
throw new IllegalArgumentException("Point has wrong dimension");
if (point.getDimension() != k) throw new IllegalArgumentException("Point has wrong dimension");
root = insert(root, point, 0);
}
@ -257,8 +253,7 @@ public class KDTree {
* @return The Node corresponding to the specified point
*/
public Optional<Node> search(Point point) {
if (point.getDimension() != k)
throw new IllegalArgumentException("Point has wrong dimension");
if (point.getDimension() != k) throw new IllegalArgumentException("Point has wrong dimension");
return search(root, point);
}
@ -304,10 +299,7 @@ public class KDTree {
Node left = findMin(root.left, axis);
Node right = findMin(root.right, axis);
Node[] candidates = {left, root, right};
return Arrays.stream(candidates)
.filter(Objects::nonNull)
.min(Comparator.comparingInt(a -> a.point.getCoordinate(axis)))
.orElse(null);
return Arrays.stream(candidates).filter(Objects::nonNull).min(Comparator.comparingInt(a -> a.point.getCoordinate(axis))).orElse(null);
}
}
@ -339,10 +331,7 @@ public class KDTree {
Node left = findMax(root.left, axis);
Node right = findMax(root.right, axis);
Node[] candidates = {left, root, right};
return Arrays.stream(candidates)
.filter(Objects::nonNull)
.max(Comparator.comparingInt(a -> a.point.getCoordinate(axis)))
.orElse(null);
return Arrays.stream(candidates).filter(Objects::nonNull).max(Comparator.comparingInt(a -> a.point.getCoordinate(axis))).orElse(null);
}
}
@ -352,8 +341,7 @@ public class KDTree {
* @param point the point to delete
* */
public void delete(Point point) {
Node node
= search(point).orElseThrow(() -> new IllegalArgumentException("Point not found"));
Node node = search(point).orElseThrow(() -> new IllegalArgumentException("Point not found"));
root = delete(root, node);
}
@ -406,12 +394,10 @@ public class KDTree {
if (root == null) return nearest;
if (root.point.equals(point)) return root;
int distance = Point.comparableDistance(root.point, point);
int distanceExceptAxis
= Point.comparableDistanceExceptAxis(root.point, point, root.getAxis());
int distanceExceptAxis = Point.comparableDistanceExceptAxis(root.point, point, root.getAxis());
if (distance < Point.comparableDistance(nearest.point, point)) nearest = root;
nearest = findNearest(root.getNearChild(point), point, nearest);
if (distanceExceptAxis < Point.comparableDistance(nearest.point, point))
nearest = findNearest(root.getFarChild(point), point, nearest);
if (distanceExceptAxis < Point.comparableDistance(nearest.point, point)) nearest = findNearest(root.getFarChild(point), point, nearest);
return nearest;
}
}

View File

@ -53,8 +53,7 @@ public class LCA {
* @param parent An array to store parents of all vertices
* @param depth An array to store depth of all vertices
*/
private static void dfs(
ArrayList<ArrayList<Integer>> adj, int s, int p, int[] parent, int[] depth) {
private static void dfs(ArrayList<ArrayList<Integer>> adj, int s, int p, int[] parent, int[] depth) {
for (int adjacent : adj.get(s)) {
if (adjacent != p) {
parent[adjacent] = s;

View File

@ -28,8 +28,7 @@ public class RedBlackBST {
return;
}
printTree(node.left);
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key
+ " Parent: " + node.p.key + "\n");
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
printTree(node.right);
}
@ -37,8 +36,7 @@ public class RedBlackBST {
if (node == nil) {
return;
}
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key
+ " Parent: " + node.p.key + "\n");
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
printTreepre(node.left);
printTreepre(node.right);
}

View File

@ -26,8 +26,7 @@ public class SegmentTree {
}
int mid = start + (end - start) / 2;
this.seg_t[index] = constructTree(arr, start, mid, index * 2 + 1)
+ constructTree(arr, mid + 1, end, index * 2 + 2);
this.seg_t[index] = constructTree(arr, start, mid, index * 2 + 1) + constructTree(arr, mid + 1, end, index * 2 + 2);
return this.seg_t[index];
}
@ -69,8 +68,7 @@ public class SegmentTree {
}
int mid = start + (end - start) / 2;
return (getSumTree(start, mid, q_start, q_end, seg_index * 2 + 1)
+ getSumTree(mid + 1, end, q_start, q_end, seg_index * 2 + 2));
return (getSumTree(start, mid, q_start, q_end, seg_index * 2 + 1) + getSumTree(mid + 1, end, q_start, q_end, seg_index * 2 + 2));
}
/* A function to query the sum of the subarray [start...end]*/