mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Code cleanup (#4246)
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
package com.thealgorithms.conversions;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Converts any Octal Number to a Binary Number
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
|
@ -28,18 +28,12 @@ public class DoublyLinkedList {
|
||||
*/
|
||||
private LinkOperations linkOperations;
|
||||
|
||||
/**
|
||||
* Size refers to the number of elements present in the list
|
||||
*/
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*/
|
||||
public DoublyLinkedList() {
|
||||
head = null;
|
||||
tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +49,6 @@ public class DoublyLinkedList {
|
||||
for (int i : array) {
|
||||
linkOperations.insertTail(i, this);
|
||||
}
|
||||
size = array.length;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,10 +47,5 @@ public class Merge_K_SortedLinkedlist {
|
||||
|
||||
private int data;
|
||||
private Node next;
|
||||
|
||||
public Node(int d) {
|
||||
this.data = d;
|
||||
next = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -148,9 +148,7 @@ public class SinglyLinkedList extends Node {
|
||||
public void clear() {
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
Node prev = cur;
|
||||
cur = cur.next;
|
||||
prev = null; // clear to let GC do its work
|
||||
}
|
||||
head = null;
|
||||
size = 0;
|
||||
@ -346,9 +344,7 @@ public class SinglyLinkedList extends Node {
|
||||
public void deleteNth(int position) {
|
||||
checkBounds(position, 0, size - 1);
|
||||
if (position == 0) {
|
||||
Node destroy = head;
|
||||
head = head.next;
|
||||
destroy = null;
|
||||
/* clear to let GC do its work */
|
||||
size--;
|
||||
return;
|
||||
@ -358,10 +354,7 @@ public class SinglyLinkedList extends Node {
|
||||
cur = cur.next;
|
||||
}
|
||||
|
||||
Node destroy = cur.next;
|
||||
cur.next = cur.next.next;
|
||||
destroy = null; // clear to let GC do its work
|
||||
|
||||
size--;
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,6 @@ public class NodeStack<Item> {
|
||||
private Item data;
|
||||
|
||||
private static NodeStack<?> head;
|
||||
private NodeStack<?> next;
|
||||
private NodeStack<?> previous;
|
||||
private static int size = 0;
|
||||
|
||||
@ -72,7 +71,7 @@ public class NodeStack<Item> {
|
||||
} else {
|
||||
newNs.setPrevious(NodeStack.head);
|
||||
NodeStack.head.setNext(newNs);
|
||||
NodeStack.head.setHead(newNs);
|
||||
NodeStack.setHead(newNs);
|
||||
}
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() + 1);
|
||||
@ -86,7 +85,7 @@ public class NodeStack<Item> {
|
||||
public Item pop() {
|
||||
Item item = (Item) NodeStack.head.getData();
|
||||
|
||||
NodeStack.head.setHead(NodeStack.head.getPrevious());
|
||||
NodeStack.setHead(NodeStack.head.getPrevious());
|
||||
NodeStack.head.setNext(null);
|
||||
|
||||
NodeStack.setSize(NodeStack.getSize() - 1);
|
||||
@ -133,23 +132,11 @@ public class NodeStack<Item> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getters and setters (private)
|
||||
*/
|
||||
private NodeStack<?> getHead() {
|
||||
return NodeStack.head;
|
||||
}
|
||||
|
||||
private static void setHead(NodeStack<?> ns) {
|
||||
NodeStack.head = ns;
|
||||
}
|
||||
|
||||
private NodeStack<?> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
private void setNext(NodeStack<?> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
private NodeStack<?> getPrevious() {
|
||||
@ -171,8 +158,4 @@ public class NodeStack<Item> {
|
||||
private Item getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
private void setData(Item item) {
|
||||
this.data = item;
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,6 @@ public class GenericTree {
|
||||
}
|
||||
|
||||
private Node root;
|
||||
private int size;
|
||||
|
||||
public GenericTree() { // Constructor
|
||||
Scanner scn = new Scanner(System.in);
|
||||
root = create_treeG(null, 0, scn);
|
||||
@ -44,7 +42,6 @@ public class GenericTree {
|
||||
int number = scn.nextInt();
|
||||
for (int i = 0; i < number; i++) {
|
||||
Node child = create_treeG(node, i, scn);
|
||||
size++;
|
||||
node.child.add(child);
|
||||
}
|
||||
return node;
|
||||
|
@ -30,11 +30,6 @@ public class TreeRandomNode {
|
||||
|
||||
int item;
|
||||
Node left, right;
|
||||
|
||||
public Node(int key) {
|
||||
item = key;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Using an arraylist to store the inorder traversal of the given binary tree
|
||||
|
@ -12,6 +12,7 @@ class Main {
|
||||
int inputX0 = sc.nextInt();
|
||||
int toPrint = nearestRightKey(root, inputX0);
|
||||
System.out.println("Key: " + toPrint);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
public static NRKTree BuildTree() {
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Given a string containing just the characters '(' and ')', find the length of
|
||||
* the longest valid (well-formed) parentheses substring.
|
||||
|
@ -51,5 +51,6 @@ public class DeterminantOfMatrix {
|
||||
}
|
||||
}
|
||||
System.out.println(determinant(a, n));
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,6 @@
|
||||
*/
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class DudeneyNumber {
|
||||
|
||||
// returns True if the number is a Dudeney number and False if it is not a Dudeney number.
|
||||
|
@ -49,5 +49,6 @@ class KeithNumber {
|
||||
} else {
|
||||
System.out.println("No, the given number is not a Keith number.");
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ public class LeastCommonMultiple {
|
||||
System.out.println("Please enter second number >> ");
|
||||
int num2 = input.nextInt();
|
||||
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
|
||||
input.close();
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -45,5 +45,6 @@ public class MagicSquare {
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
@ -50,6 +50,7 @@ public class NonRepeatingElement {
|
||||
}
|
||||
|
||||
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
|
||||
sc.close();
|
||||
}
|
||||
/*
|
||||
Explanation of the code:
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/*
|
||||
*To learn about the method, visit the link below :
|
||||
* https://en.wikipedia.org/wiki/Newton%27s_method
|
||||
|
@ -11,9 +11,6 @@ public class MinimizingLateness {
|
||||
|
||||
int t = 0; // Time required for the operation to be performed
|
||||
int d = 0; // Time the job should be completed
|
||||
int s = 0; // Start time of the task
|
||||
int f = 0; // End time of the operation
|
||||
|
||||
public Schedule(int t, int d) {
|
||||
this.t = t;
|
||||
this.d = d;
|
||||
@ -46,8 +43,6 @@ public class MinimizingLateness {
|
||||
int tryTime = 0; // Total time worked
|
||||
int lateness = 0; // Lateness
|
||||
for (int j = 0; j < indexCount - 1; j++) {
|
||||
array[j].s = tryTime; // Start time of the task
|
||||
array[j].f = tryTime + array[j].t; // Time finished
|
||||
tryTime = tryTime + array[j].t; // Add total work time
|
||||
// Lateness
|
||||
lateness = lateness + Math.max(0, tryTime - array[j].d);
|
||||
|
@ -21,6 +21,7 @@ public class Sort012D {
|
||||
a[i] = np.nextInt();
|
||||
}
|
||||
sort012(a);
|
||||
np.close();
|
||||
}
|
||||
|
||||
public static void sort012(int[] a) {
|
||||
|
@ -49,5 +49,6 @@ class Sparcity {
|
||||
}
|
||||
}
|
||||
System.out.println("Sparcity of matrix is: " + sparcity(mat));
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ public class ThreeSumProblem {
|
||||
System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n");
|
||||
System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n");
|
||||
System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts)));
|
||||
scan.close();
|
||||
}
|
||||
|
||||
public List<List<Integer>> BruteForce(int[] nums, int target) {
|
||||
|
@ -44,5 +44,6 @@ public class BoyerMoore {
|
||||
a[i] = input.nextInt();
|
||||
}
|
||||
System.out.println("the majority element is " + findmajor(a));
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author Marcus
|
||||
*/
|
||||
|
@ -19,6 +19,7 @@ public class HappyNumbersSeq {
|
||||
}
|
||||
String res = n == 1 ? "1 Happy number" : "Sad number";
|
||||
System.out.println(res);
|
||||
in.close();
|
||||
}
|
||||
|
||||
private static int sumSquares(int n) {
|
||||
|
@ -118,5 +118,6 @@ public class Huffman {
|
||||
|
||||
// print the codes by traversing the tree
|
||||
printCode(root, "");
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
|
@ -22,8 +22,7 @@ class Rotate_by_90_degree {
|
||||
}
|
||||
}
|
||||
|
||||
Rotate g = new Rotate();
|
||||
g.rotate(arr);
|
||||
Rotate.rotate(arr);
|
||||
printMatrix(arr);
|
||||
}
|
||||
sc.close();
|
||||
|
@ -1,8 +1,5 @@
|
||||
package com.thealgorithms.others.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
final public class HammingDistance {
|
||||
private HammingDistance() {
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ public class LinearSearchThread {
|
||||
}
|
||||
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
|
||||
System.out.println("Found = " + found);
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,9 +21,8 @@ class PerfectBinarySearch {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
PerfectBinarySearch BinarySearch = new PerfectBinarySearch();
|
||||
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
assert BinarySearch.binarySearch(array, -1) == -1;
|
||||
assert BinarySearch.binarySearch(array, 11) == -1;
|
||||
assert PerfectBinarySearch.binarySearch(array, -1) == -1;
|
||||
assert PerfectBinarySearch.binarySearch(array, 11) == -1;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SearchInARowAndColWiseSortedMatrix {
|
||||
/**
|
||||
* Search a key in row and column wise sorted matrix
|
||||
|
@ -27,6 +27,7 @@ public class SquareRootBinarySearch {
|
||||
int num = sc.nextInt();
|
||||
long ans = squareRoot(num);
|
||||
System.out.println("The square root is : " + ans);
|
||||
sc.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,4 @@
|
||||
package com.thealgorithms.searches;
|
||||
import java.util.*;
|
||||
public class sortOrderAgnosticBinarySearch {
|
||||
public static int find(int[] arr, int key) {
|
||||
int start = 0;
|
||||
|
@ -66,5 +66,6 @@ public class MergeSortNoExtraSpace {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
System.out.print(a[i] + " ");
|
||||
}
|
||||
inp.close();
|
||||
}
|
||||
}
|
||||
|
@ -33,22 +33,6 @@ public class TopologicalSort {
|
||||
* */
|
||||
public final String label;
|
||||
|
||||
/*
|
||||
* Weight of vertex
|
||||
* (more accurately defined as the time that a vertex has begun a visit in DFS)
|
||||
* */
|
||||
public int weight;
|
||||
|
||||
/*
|
||||
* The time that the vertex has finished a visit in DFS
|
||||
* */
|
||||
public int finished;
|
||||
|
||||
/*
|
||||
* π parent of the vertex
|
||||
* */
|
||||
public Vertex predecessor;
|
||||
|
||||
/*
|
||||
* Represents the category of visit in DFS
|
||||
* */
|
||||
@ -90,11 +74,6 @@ public class TopologicalSort {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Time variable in DFS
|
||||
* */
|
||||
private static int time;
|
||||
|
||||
/*
|
||||
* Depth First Search
|
||||
*
|
||||
@ -135,12 +114,9 @@ public class TopologicalSort {
|
||||
* u.f = time
|
||||
* */
|
||||
private static String sort(Graph graph, Vertex u, LinkedList<String> list) {
|
||||
time++;
|
||||
u.weight = time;
|
||||
u.color = Color.GRAY;
|
||||
graph.adj.get(u.label).next.forEach(label -> {
|
||||
if (graph.adj.get(label).color == Color.WHITE) {
|
||||
graph.adj.get(label).predecessor = u;
|
||||
list.addFirst(sort(graph, graph.adj.get(label), list));
|
||||
} else if (graph.adj.get(label).color == Color.GRAY) {
|
||||
/*
|
||||
@ -153,8 +129,6 @@ public class TopologicalSort {
|
||||
}
|
||||
});
|
||||
u.color = Color.BLACK;
|
||||
time++;
|
||||
u.finished = time;
|
||||
return u.label;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ class LongestPalindromicSubstring {
|
||||
System.out.print("Enter the string: ");
|
||||
str = sc.nextLine();
|
||||
System.out.println("Longest substring is : " + s.longestPalindrome(str));
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
Reference in New Issue
Block a user