mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 14:12:36 +08:00
Close Scanner
to avoid resource leak (#5077)
This commit is contained in:
@ -5,7 +5,7 @@ import java.util.Scanner;
|
|||||||
class ProductCipher {
|
class ProductCipher {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
try (Scanner sc = new Scanner(System.in)) {
|
||||||
System.out.println("Enter the input to be encrypted: ");
|
System.out.println("Enter the input to be encrypted: ");
|
||||||
String substitutionInput = sc.nextLine();
|
String substitutionInput = sc.nextLine();
|
||||||
System.out.println(" ");
|
System.out.println(" ");
|
||||||
@ -68,4 +68,5 @@ class ProductCipher {
|
|||||||
System.out.println(plaintext);
|
System.out.println(plaintext);
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,13 @@ package com.thealgorithms.datastructures.graphs;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs
|
class BellmanFord /*
|
||||||
in form of edges which have start vertex, end vertex and weights. Vertices should be labelled with a
|
* Implementation of Bellman ford to detect negative cycles. Graph accepts
|
||||||
number between 0 and total number of vertices-1,both inclusive*/
|
* inputs
|
||||||
|
* in form of edges which have start vertex, end vertex and weights. Vertices
|
||||||
|
* should be labelled with a
|
||||||
|
* number between 0 and total number of vertices-1,both inclusive
|
||||||
|
*/
|
||||||
{
|
{
|
||||||
|
|
||||||
int vertex, edge;
|
int vertex, edge;
|
||||||
@ -52,8 +56,8 @@ number between 0 and total number of vertices-1,both inclusive*/
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void go() { // shows distance to all vertices // Interactive run for understanding the
|
public void go() { // shows distance to all vertices // Interactive run for understanding the
|
||||||
// class first time. Assumes source vertex is 0 and
|
try ( // class first time. Assumes source vertex is 0 and
|
||||||
Scanner sc = new Scanner(System.in); // Grab scanner object for user input
|
Scanner sc = new Scanner(System.in)) {
|
||||||
int i, v, e, u, ve, w, j, neg = 0;
|
int i, v, e, u, ve, w, j, neg = 0;
|
||||||
System.out.println("Enter no. of vertices and edges please");
|
System.out.println("Enter no. of vertices and edges please");
|
||||||
v = sc.nextInt();
|
v = sc.nextInt();
|
||||||
@ -105,6 +109,7 @@ number between 0 and total number of vertices-1,both inclusive*/
|
|||||||
}
|
}
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param source Starting vertex
|
* @param source Starting vertex
|
||||||
|
@ -11,7 +11,7 @@ import java.util.Stack;
|
|||||||
public class ReverseStack {
|
public class ReverseStack {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
try (Scanner sc = new Scanner(System.in)) {
|
||||||
System.out.println("Enter the number of elements you wish to insert in the stack");
|
System.out.println("Enter the number of elements you wish to insert in the stack");
|
||||||
int n = sc.nextInt();
|
int n = sc.nextInt();
|
||||||
int i;
|
int i;
|
||||||
@ -28,6 +28,7 @@ public class ReverseStack {
|
|||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void reverseStack(Stack<Integer> stack) {
|
private static void reverseStack(Stack<Integer> stack) {
|
||||||
if (stack.isEmpty()) {
|
if (stack.isEmpty()) {
|
||||||
@ -48,16 +49,15 @@ public class ReverseStack {
|
|||||||
|
|
||||||
private static void insertAtBottom(Stack<Integer> stack, int element) {
|
private static void insertAtBottom(Stack<Integer> stack, int element) {
|
||||||
if (stack.isEmpty()) {
|
if (stack.isEmpty()) {
|
||||||
// When stack is empty, insert the element so it will be present at the bottom of the
|
// When stack is empty, insert the element so it will be present at
|
||||||
// stack
|
// the bottom of the stack
|
||||||
stack.push(element);
|
stack.push(element);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ele = stack.peek();
|
int ele = stack.peek();
|
||||||
/*Keep popping elements till stack becomes empty. Push the elements once the topmost element
|
// Keep popping elements till stack becomes empty. Push the elements
|
||||||
has moved to the bottom of the stack.
|
// once the topmost element has moved to the bottom of the stack.
|
||||||
*/
|
|
||||||
stack.pop();
|
stack.pop();
|
||||||
insertAtBottom(stack, element);
|
insertAtBottom(stack, element);
|
||||||
|
|
||||||
|
@ -10,12 +10,13 @@ import java.util.Scanner;
|
|||||||
public class NonRepeatingElement {
|
public class NonRepeatingElement {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
try (Scanner sc = new Scanner(System.in)) {
|
||||||
int i, res = 0;
|
int i, res = 0;
|
||||||
System.out.println("Enter the number of elements in the array");
|
System.out.println("Enter the number of elements in the array");
|
||||||
int n = sc.nextInt();
|
int n = sc.nextInt();
|
||||||
if ((n & 1) == 1) {
|
if ((n & 1) == 1) {
|
||||||
// Not allowing odd number of elements as we are expecting 2 non repeating numbers
|
// Not allowing odd number of elements as we are expecting 2 non repeating
|
||||||
|
// numbers
|
||||||
System.out.println("Array should contain even number of elements");
|
System.out.println("Array should contain even number of elements");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -52,19 +53,27 @@ public class NonRepeatingElement {
|
|||||||
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
|
System.out.println("The two non repeating elements are " + num1 + " and " + num2);
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
Explanation of the code:
|
* Explanation of the code:
|
||||||
let us assume we have an array [1,2,1,2,3,4]
|
* let us assume we have an array [1,2,1,2,3,4]
|
||||||
Property of XOR: num ^ num = 0.
|
* Property of XOR: num ^ num = 0.
|
||||||
If we XOR all the elemnets of the array we will be left with 3 ^ 4 as 1 ^ 1 and 2 ^ 2 would give
|
* If we XOR all the elemnets of the array we will be left with 3 ^ 4 as 1 ^ 1
|
||||||
0. Our task is to find num1 and num2 from the result of 3 ^ 4 = 7. We need to find two's
|
* and 2 ^ 2 would give
|
||||||
complement of 7 and find the rightmost set bit. i.e. (num & (-num)) Two's complement of 7 is 001
|
* 0. Our task is to find num1 and num2 from the result of 3 ^ 4 = 7. We need to
|
||||||
and hence res = 1. There can be 2 options when we Bitise AND this res with all the elements in our
|
* find two's
|
||||||
array
|
* complement of 7 and find the rightmost set bit. i.e. (num & (-num)) Two's
|
||||||
1. Result will come non zero number
|
* complement of 7 is 001
|
||||||
2. Result will be 0.
|
* and hence res = 1. There can be 2 options when we Bitise AND this res with
|
||||||
In the first case we will XOR our element with the first number (which is initially 0)
|
* all the elements in our
|
||||||
In the second case we will XOR our element with the second number(which is initially 0)
|
* array
|
||||||
This is how we will get non repeating elements with the help of bitwise operators.
|
* 1. Result will come non zero number
|
||||||
|
* 2. Result will be 0.
|
||||||
|
* In the first case we will XOR our element with the first number (which is
|
||||||
|
* initially 0)
|
||||||
|
* In the second case we will XOR our element with the second number(which is
|
||||||
|
* initially 0)
|
||||||
|
* This is how we will get non repeating elements with the help of bitwise
|
||||||
|
* operators.
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import java.util.*;
|
|||||||
public class InsertDeleteInArray {
|
public class InsertDeleteInArray {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner s = new Scanner(System.in); // Input statement
|
try (Scanner s = new Scanner(System.in)) {
|
||||||
System.out.println("Enter the size of the array");
|
System.out.println("Enter the size of the array");
|
||||||
int size = s.nextInt();
|
int size = s.nextInt();
|
||||||
int[] a = new int[size];
|
int[] a = new int[size];
|
||||||
@ -47,4 +47,5 @@ public class InsertDeleteInArray {
|
|||||||
}
|
}
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// File Name should be RecursiveBinarySearch.java
|
// File Name should be RecursiveBinarySearch.java
|
||||||
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
|
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive
|
||||||
package com.thealgorithms.searches;
|
package com.thealgorithms.searches;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
// Create a SearchAlgorithm class with a generic type
|
// Create a SearchAlgorithm class with a generic type
|
||||||
@ -47,7 +48,7 @@ public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgori
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
try (Scanner sc = new Scanner(System.in)) {
|
||||||
// User inputs
|
// User inputs
|
||||||
System.out.print("Enter the number of elements in the array: ");
|
System.out.print("Enter the number of elements in the array: ");
|
||||||
int n = sc.nextInt();
|
int n = sc.nextInt();
|
||||||
@ -71,4 +72,5 @@ public class RecursiveBinarySearch<T extends Comparable<T>> extends SearchAlgori
|
|||||||
else
|
else
|
||||||
System.out.println("Element found at index " + res);
|
System.out.println("Element found at index " + res);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user