mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
refactor: Enhance docs, add tests in PrintMatrixInSpiralOrder (#6636)
* refactor: Enhance docs, add tests in `PrintMatrixInSpiralOrder` * Fix error in BloomFilter * Fix * Fix * Fix
This commit is contained in:
@@ -6,8 +6,10 @@ import java.util.BitSet;
|
||||
/**
|
||||
* A generic BloomFilter implementation for probabilistic membership checking.
|
||||
* <p>
|
||||
* Bloom filters are space-efficient data structures that provide a fast way to test whether an
|
||||
* element is a member of a set. They may produce false positives, indicating an element is
|
||||
* Bloom filters are space-efficient data structures that provide a fast way to
|
||||
* test whether an
|
||||
* element is a member of a set. They may produce false positives, indicating an
|
||||
* element is
|
||||
* in the set when it is not, but they will never produce false negatives.
|
||||
* </p>
|
||||
*
|
||||
@@ -21,11 +23,14 @@ public class BloomFilter<T> {
|
||||
private final Hash<T>[] hashFunctions;
|
||||
|
||||
/**
|
||||
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
|
||||
* Constructs a BloomFilter with a specified number of hash functions and bit
|
||||
* array size.
|
||||
*
|
||||
* @param numberOfHashFunctions the number of hash functions to use
|
||||
* @param bitArraySize the size of the bit array, which determines the capacity of the filter
|
||||
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1
|
||||
* @param bitArraySize the size of the bit array, which determines the
|
||||
* capacity of the filter
|
||||
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is
|
||||
* less than 1
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
|
||||
@@ -39,7 +44,8 @@ public class BloomFilter<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the hash functions with unique indices to ensure different hashing.
|
||||
* Initializes the hash functions with unique indices to ensure different
|
||||
* hashing.
|
||||
*/
|
||||
private void initializeHashFunctions() {
|
||||
for (int i = 0; i < numberOfHashFunctions; i++) {
|
||||
@@ -50,7 +56,8 @@ public class BloomFilter<T> {
|
||||
/**
|
||||
* Inserts an element into the Bloom filter.
|
||||
* <p>
|
||||
* This method hashes the element using all defined hash functions and sets the corresponding
|
||||
* This method hashes the element using all defined hash functions and sets the
|
||||
* corresponding
|
||||
* bits in the bit array.
|
||||
* </p>
|
||||
*
|
||||
@@ -66,13 +73,16 @@ public class BloomFilter<T> {
|
||||
/**
|
||||
* Checks if an element might be in the Bloom filter.
|
||||
* <p>
|
||||
* This method checks the bits at the positions computed by each hash function. If any of these
|
||||
* bits are not set, the element is definitely not in the filter. If all bits are set, the element
|
||||
* This method checks the bits at the positions computed by each hash function.
|
||||
* If any of these
|
||||
* bits are not set, the element is definitely not in the filter. If all bits
|
||||
* are set, the element
|
||||
* might be in the filter.
|
||||
* </p>
|
||||
*
|
||||
* @param key the element to check for membership in the Bloom filter
|
||||
* @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not
|
||||
* @return {@code true} if the element might be in the Bloom filter,
|
||||
* {@code false} if it is definitely not
|
||||
*/
|
||||
public boolean contains(T key) {
|
||||
for (Hash<T> hash : hashFunctions) {
|
||||
@@ -87,7 +97,8 @@ public class BloomFilter<T> {
|
||||
/**
|
||||
* Inner class representing a hash function used by the Bloom filter.
|
||||
* <p>
|
||||
* Each instance of this class represents a different hash function based on its index.
|
||||
* Each instance of this class represents a different hash function based on its
|
||||
* index.
|
||||
* </p>
|
||||
*
|
||||
* @param <T> The type of elements to be hashed.
|
||||
|
||||
@@ -3,17 +3,41 @@ package com.thealgorithms.matrix;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Utility class to print a matrix in spiral order.
|
||||
* <p>
|
||||
* Given a 2D array (matrix), this class provides a method to return the
|
||||
* elements
|
||||
* of the matrix in spiral order, starting from the top-left corner and moving
|
||||
* clockwise.
|
||||
* </p>
|
||||
*
|
||||
* @author Sadiul Hakim (https://github.com/sadiul-hakim)
|
||||
*/
|
||||
public class PrintAMatrixInSpiralOrder {
|
||||
|
||||
/**
|
||||
* Search a key in row and column wise sorted matrix
|
||||
* Returns the elements of the given matrix in spiral order.
|
||||
*
|
||||
* @param matrix matrix to be searched
|
||||
* @param row number of rows matrix has
|
||||
* @param col number of columns matrix has
|
||||
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
* @param matrix the 2D array to traverse in spiral order
|
||||
* @param row the number of rows in the matrix
|
||||
* @param col the number of columns in the matrix
|
||||
* @return a list containing the elements of the matrix in spiral order
|
||||
*
|
||||
* <p>
|
||||
* Example:
|
||||
*
|
||||
* <pre>
|
||||
* int[][] matrix = {
|
||||
* {1, 2, 3},
|
||||
* {4, 5, 6},
|
||||
* {7, 8, 9}
|
||||
* };
|
||||
* print(matrix, 3, 3) returns [1, 2, 3, 6, 9, 8, 7, 4, 5]
|
||||
* </pre>
|
||||
* </p>
|
||||
*/
|
||||
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||
|
||||
// r traverses matrix row wise from first
|
||||
int r = 0;
|
||||
// c traverses matrix column wise from first
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PrintAMatrixInSpiralOrder {
|
||||
/**
|
||||
* Search a key in row and column wise sorted matrix
|
||||
*
|
||||
* @param matrix matrix to be searched
|
||||
* @param row number of rows matrix has
|
||||
* @param col number of columns matrix has
|
||||
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
*/
|
||||
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||
// r traverses matrix row wise from first
|
||||
int r = 0;
|
||||
// c traverses matrix column wise from first
|
||||
int c = 0;
|
||||
int i;
|
||||
List<Integer> result = new ArrayList<>();
|
||||
while (r < row && c < col) {
|
||||
// print first row of matrix
|
||||
for (i = c; i < col; i++) {
|
||||
result.add(matrix[r][i]);
|
||||
}
|
||||
// increase r by one because first row printed
|
||||
r++;
|
||||
// print last column
|
||||
for (i = r; i < row; i++) {
|
||||
result.add(matrix[i][col - 1]);
|
||||
}
|
||||
// decrease col by one because last column has been printed
|
||||
col--;
|
||||
// print rows from last except printed elements
|
||||
if (r < row) {
|
||||
for (i = col - 1; i >= c; i--) {
|
||||
result.add(matrix[row - 1][i]);
|
||||
}
|
||||
row--;
|
||||
}
|
||||
// print columns from first except printed elements
|
||||
if (c < col) {
|
||||
for (i = row - 1; i >= r; i--) {
|
||||
result.add(matrix[i][c]);
|
||||
}
|
||||
c++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user