Cleaned up code for some packages (#5094)
* Cleaned up code of some packages --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
parent
40cd4d86ef
commit
22310defcd
@ -9,7 +9,7 @@ package com.thealgorithms.conversions;
|
|||||||
*/
|
*/
|
||||||
public class IntegerToRoman {
|
public class IntegerToRoman {
|
||||||
|
|
||||||
private static int[] allArabianRomanNumbers = new int[] {
|
private static final int[] allArabianRomanNumbers = new int[] {
|
||||||
1000,
|
1000,
|
||||||
900,
|
900,
|
||||||
500,
|
500,
|
||||||
@ -24,7 +24,7 @@ public class IntegerToRoman {
|
|||||||
4,
|
4,
|
||||||
1,
|
1,
|
||||||
};
|
};
|
||||||
private static String[] allRomanNumbers = new String[] {
|
private static final String[] allRomanNumbers = new String[] {
|
||||||
"M",
|
"M",
|
||||||
"CM",
|
"CM",
|
||||||
"D",
|
"D",
|
||||||
|
@ -4,9 +4,7 @@ import java.util.*;
|
|||||||
|
|
||||||
public class RomanToInteger {
|
public class RomanToInteger {
|
||||||
|
|
||||||
private static Map<Character, Integer> map = new HashMap<Character, Integer>() {
|
private static final Map<Character, Integer> map = new HashMap<>() {
|
||||||
/**
|
|
||||||
* */
|
|
||||||
private static final long serialVersionUID = 87605733047260530L;
|
private static final long serialVersionUID = 87605733047260530L;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -58,7 +58,7 @@ public class TurkishToLatinConversion {
|
|||||||
'G',
|
'G',
|
||||||
};
|
};
|
||||||
for (int i = 0; i < turkishChars.length; i++) {
|
for (int i = 0; i < turkishChars.length; i++) {
|
||||||
param = param.replaceAll(new String(new char[] {turkishChars[i]}), new String(new char[] {latinChars[i]}));
|
param = param.replaceAll(String.valueOf(turkishChars[i]), String.valueOf(latinChars[i]));
|
||||||
}
|
}
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class Fibonacci {
|
public class Fibonacci {
|
||||||
|
|
||||||
private static Map<Integer, Integer> map = new HashMap<>();
|
private static final Map<Integer, Integer> map = new HashMap<>();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
|
||||||
@ -106,7 +106,6 @@ public class Fibonacci {
|
|||||||
public static int fibBinet(int n) {
|
public static int fibBinet(int n) {
|
||||||
double squareRootOf5 = Math.sqrt(5);
|
double squareRootOf5 = Math.sqrt(5);
|
||||||
double phi = (1 + squareRootOf5) / 2;
|
double phi = (1 + squareRootOf5) / 2;
|
||||||
int nthTerm = (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
|
return (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
|
||||||
return nthTerm;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class MatrixChainMultiplication {
|
public class MatrixChainMultiplication {
|
||||||
|
|
||||||
private static Scanner scan = new Scanner(System.in);
|
private static final Scanner scan = new Scanner(System.in);
|
||||||
private static ArrayList<Matrix> mArray = new ArrayList<>();
|
private static final ArrayList<Matrix> mArray = new ArrayList<>();
|
||||||
private static int size;
|
private static int size;
|
||||||
private static int[][] m;
|
private static int[][] m;
|
||||||
private static int[][] s;
|
private static int[][] s;
|
||||||
@ -115,9 +115,9 @@ public class MatrixChainMultiplication {
|
|||||||
|
|
||||||
class Matrix {
|
class Matrix {
|
||||||
|
|
||||||
private int count;
|
private final int count;
|
||||||
private int col;
|
private final int col;
|
||||||
private int row;
|
private final int row;
|
||||||
|
|
||||||
Matrix(int count, int col, int row) {
|
Matrix(int count, int col, int row) {
|
||||||
this.count = count;
|
this.count = count;
|
||||||
|
@ -8,9 +8,9 @@ import java.util.Comparator;
|
|||||||
|
|
||||||
public class ActivitySelection {
|
public class ActivitySelection {
|
||||||
// Function to perform activity selection
|
// Function to perform activity selection
|
||||||
public static ArrayList<Integer> activitySelection(int startTimes[], int endTimes[]) {
|
public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) {
|
||||||
int n = startTimes.length;
|
int n = startTimes.length;
|
||||||
int activities[][] = new int[n][3];
|
int[][] activities = new int[n][3];
|
||||||
|
|
||||||
// Create a 2D array to store activities and their start/end times.
|
// Create a 2D array to store activities and their start/end times.
|
||||||
// Each row: [activity index, start time, end time]
|
// Each row: [activity index, start time, end time]
|
||||||
|
@ -10,12 +10,11 @@ public class CoinChange {
|
|||||||
// Function to solve the coin change problem
|
// Function to solve the coin change problem
|
||||||
public static ArrayList<Integer> coinChangeProblem(int amount) {
|
public static ArrayList<Integer> coinChangeProblem(int amount) {
|
||||||
// Define an array of coin denominations in descending order
|
// Define an array of coin denominations in descending order
|
||||||
Integer coins[] = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
|
Integer[] coins = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
|
||||||
|
|
||||||
// Sort the coin denominations in descending order
|
// Sort the coin denominations in descending order
|
||||||
Arrays.sort(coins, Comparator.reverseOrder());
|
Arrays.sort(coins, Comparator.reverseOrder());
|
||||||
|
|
||||||
int count = 0; // Variable to keep track of the total number of coins used
|
|
||||||
ArrayList<Integer> ans = new ArrayList<>(); // List to store selected coins
|
ArrayList<Integer> ans = new ArrayList<>(); // List to store selected coins
|
||||||
|
|
||||||
// Iterate through the coin denominations
|
// Iterate through the coin denominations
|
||||||
@ -24,7 +23,6 @@ public class CoinChange {
|
|||||||
if (coins[i] <= amount) {
|
if (coins[i] <= amount) {
|
||||||
// Repeatedly subtract the coin denomination from the remaining amount
|
// Repeatedly subtract the coin denomination from the remaining amount
|
||||||
while (coins[i] <= amount) {
|
while (coins[i] <= amount) {
|
||||||
count++; // Increment the count of coins used
|
|
||||||
ans.add(coins[i]); // Add the coin to the list of selected coins
|
ans.add(coins[i]); // Add the coin to the list of selected coins
|
||||||
amount -= coins[i]; // Update the remaining amount
|
amount -= coins[i]; // Update the remaining amount
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,9 @@ import java.util.Comparator;
|
|||||||
|
|
||||||
public class FractionalKnapsack {
|
public class FractionalKnapsack {
|
||||||
// Function to perform fractional knapsack
|
// Function to perform fractional knapsack
|
||||||
public static int fractionalKnapsack(int weight[], int value[], int capacity) {
|
public static int fractionalKnapsack(int[] weight, int[] value, int capacity) {
|
||||||
// Create a 2D array to store item indices and their value-to-weight ratios.
|
// Create a 2D array to store item indices and their value-to-weight ratios.
|
||||||
double ratio[][] = new double[weight.length][2];
|
double[][] ratio = new double[weight.length][2];
|
||||||
|
|
||||||
// Populate the ratio array with item indices and their value-to-weight ratios.
|
// Populate the ratio array with item indices and their value-to-weight ratios.
|
||||||
for (int i = 0; i < weight.length; i++) {
|
for (int i = 0; i < weight.length; i++) {
|
||||||
|
@ -31,7 +31,7 @@ public class JobSequencing {
|
|||||||
Boolean[] slots = new Boolean[size];
|
Boolean[] slots = new Boolean[size];
|
||||||
Arrays.fill(slots, false);
|
Arrays.fill(slots, false);
|
||||||
|
|
||||||
int result[] = new int[size];
|
int[] result = new int[size];
|
||||||
|
|
||||||
// Iterate through jobs to find the optimal job sequence
|
// Iterate through jobs to find the optimal job sequence
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
|
@ -4,8 +4,8 @@ import java.util.*;
|
|||||||
|
|
||||||
public class UnionFind {
|
public class UnionFind {
|
||||||
|
|
||||||
private int[] p;
|
private final int[] p;
|
||||||
private int[] r;
|
private final int[] r;
|
||||||
|
|
||||||
public UnionFind(int n) {
|
public UnionFind(int n) {
|
||||||
p = new int[n];
|
p = new int[n];
|
||||||
|
@ -7,7 +7,7 @@ public class DNFSort {
|
|||||||
static void sort012(int[] a, int arr_size) {
|
static void sort012(int[] a, int arr_size) {
|
||||||
int low = 0;
|
int low = 0;
|
||||||
int high = arr_size - 1;
|
int high = arr_size - 1;
|
||||||
int mid = 0, temp = 0;
|
int mid = 0, temp;
|
||||||
while (mid <= high) {
|
while (mid <= high) {
|
||||||
switch (a[mid]) {
|
switch (a[mid]) {
|
||||||
case 0: {
|
case 0: {
|
||||||
@ -37,7 +37,7 @@ public class DNFSort {
|
|||||||
for (int i = 0; i < arr_size; i++) {
|
for (int i = 0; i < arr_size; i++) {
|
||||||
System.out.print(arr[i] + " ");
|
System.out.print(arr[i] + " ");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Driver function to check for above functions*/
|
/*Driver function to check for above functions*/
|
||||||
|
@ -25,12 +25,11 @@ public class Pangram {
|
|||||||
*/
|
*/
|
||||||
// alternative approach using Java Collection Framework
|
// alternative approach using Java Collection Framework
|
||||||
public static boolean isPangramUsingSet(String s) {
|
public static boolean isPangramUsingSet(String s) {
|
||||||
HashSet<Character> alpha = new HashSet<Character>();
|
HashSet<Character> alpha = new HashSet<>();
|
||||||
s = s.trim().toLowerCase();
|
s = s.trim().toLowerCase();
|
||||||
for (int i = 0; i < s.length(); i++)
|
for (int i = 0; i < s.length(); i++)
|
||||||
if (s.charAt(i) != ' ') alpha.add(s.charAt(i));
|
if (s.charAt(i) != ' ') alpha.add(s.charAt(i));
|
||||||
if (alpha.size() == 26) return true;
|
return alpha.size() == 26;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user