mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
File clean-up
This commit is contained in:
42
Misc/CountChar.java
Normal file
42
Misc/CountChar.java
Normal file
@@ -0,0 +1,42 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
/**
|
||||
* @author Kyler Smith, 2017
|
||||
*
|
||||
* Implementation of a character count.
|
||||
* (Slow, could be improved upon, effectively O(n).
|
||||
* */
|
||||
|
||||
public class CountChar {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter your text: ");
|
||||
String str = input.nextLine();
|
||||
|
||||
System.out.println("There are " + CountCharacters(str) + " characters.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param str: String to count the characters
|
||||
*
|
||||
* @return int: Number of characters in the passed string
|
||||
* */
|
||||
|
||||
public static int CountCharacters(String str) {
|
||||
|
||||
int count = 0;
|
||||
|
||||
if(str.isEmpty() || str == null)
|
||||
return -1;
|
||||
|
||||
for(int i = 0; i < str.length(); i++)
|
||||
if(!Character.isWhitespace(str.charAt(i)))
|
||||
count++;
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
56
Misc/Dijkshtra.java
Normal file
56
Misc/Dijkshtra.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
@author : Mayank K Jha
|
||||
|
||||
*/
|
||||
|
||||
|
||||
public class Solution {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Scanner in =new Scanner(System.in);
|
||||
|
||||
int n=in.nextInt(); //n = Number of nodes or vertices
|
||||
int m=in.nextInt(); //m = Number of Edges
|
||||
long w[][]=new long [n+1][n+1]; //Adjacency Matrix
|
||||
|
||||
//Initializing Matrix with Certain Maximum Value for path b/w any two vertices
|
||||
for (long[] row: w)
|
||||
Arrays.fill(row, 1000000l);
|
||||
//From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l
|
||||
//For simplicity , We can also take path Value = Long.MAX_VALUE , but i have taken Max Value = 1000000l .
|
||||
|
||||
//Taking Input as Edge Location b/w a pair of vertices
|
||||
for(int i=0;i<m;i++){
|
||||
int x=in.nextInt(),y=in.nextInt();
|
||||
long cmp=in.nextLong();
|
||||
if(w[x][y]>cmp){ //Comparing previous edge value with current value - Cycle Case
|
||||
w[x][y]=cmp; w[y][x]=cmp;
|
||||
}
|
||||
}
|
||||
|
||||
//Implementing Dijkshtra's Algorithm
|
||||
|
||||
Stack <Integer> t=new Stack<Integer>();
|
||||
int src=in.nextInt();
|
||||
for(int i=1;i<=n;i++){
|
||||
if(i!=src){t.push(i);}}
|
||||
Stack <Integer> p=new Stack<Integer>();
|
||||
p.push(src);
|
||||
w[src][src]=0;
|
||||
while(!t.isEmpty()){int min=989997979,loc=-1;
|
||||
for(int i=0;i<t.size();i++){
|
||||
w[src][t.elementAt(i)]=Math.min(w[src][t.elementAt(i)],w[src][p.peek()]
|
||||
+w[p.peek()][t.elementAt(i)]);
|
||||
if(w[src][t.elementAt(i)]<=min){
|
||||
min=(int) w[src][t.elementAt(i)];loc=i;}
|
||||
}
|
||||
p.push(t.elementAt(loc));t.removeElementAt(loc);}
|
||||
|
||||
//Printing shortest path from the given source src
|
||||
for(int i=1;i<=n;i++){
|
||||
if(i!=src && w[src][i]!=1000000l){System.out.print(w[src][i]+" ");}
|
||||
else if(i!=src){System.out.print("-1"+" ");} //Printing -1 if there is no path b/w given pair of edges
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
50
Misc/Factorial.java
Normal file
50
Misc/Factorial.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package factorial;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This program will print out the factorial of any non-negative
|
||||
* number that you input into it.
|
||||
*
|
||||
* @author Marcus
|
||||
*
|
||||
*/
|
||||
public class Factorial{
|
||||
|
||||
/**
|
||||
* The main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter a non-negative integer: ");
|
||||
|
||||
//If user does not enter an Integer, we want program to fail gracefully, letting the user know why it terminated
|
||||
try{
|
||||
int number = input.nextInt();
|
||||
|
||||
//We keep prompting the user until they enter a positive number
|
||||
while(number < 0){
|
||||
System.out.println("Your input must be non-negative. Please enter a positive number: ");
|
||||
number = input.nextInt();
|
||||
}
|
||||
//Display the result
|
||||
System.out.println("The factorial of " + number + " will yield: " + factorial(number));
|
||||
|
||||
}catch(Exception e){
|
||||
System.out.println("Error: You did not enter an integer. Program has terminated.");
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive Factorial Method
|
||||
*
|
||||
* @param n The number to factorial
|
||||
* @return The factorial of the number
|
||||
*/
|
||||
public static long factorial(int n){
|
||||
if(n == 0 || n == 1) return 1;
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
}
|
||||
45
Misc/FindingPrimes.java
Normal file
45
Misc/FindingPrimes.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
|
||||
* up to a given value.
|
||||
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||
* (This illustration is also in the github repository)
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class FindingPrimes{
|
||||
/**
|
||||
* The Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
SOE(20); //Example: Finds all the primes up to 20
|
||||
}
|
||||
|
||||
/**
|
||||
* The method implementing the Sieve of Eratosthenes
|
||||
*
|
||||
* @param n Number to perform SOE on
|
||||
*/
|
||||
public static void SOE(int n){
|
||||
boolean sieve[] = new boolean[n];
|
||||
|
||||
int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n
|
||||
|
||||
sieve[0] = false;
|
||||
sieve[1] = false;
|
||||
for(int i = 2; i < n; i++)
|
||||
sieve[i] = true; //Set every index to true except index 0 and 1
|
||||
|
||||
for(int i = 2; i< check; i++){
|
||||
if(sieve[i]==true) //If i is a prime
|
||||
for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime)
|
||||
sieve[j] = false; //Set every multiple of i to false
|
||||
}
|
||||
for(int i = 0; i< n; i++){
|
||||
if(sieve[i]==true)
|
||||
System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Misc/ReverseString.java
Normal file
46
Misc/ReverseString.java
Normal file
@@ -0,0 +1,46 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This method produces a reversed version of a string
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class ReverseString
|
||||
{
|
||||
|
||||
/**
|
||||
* This method reverses the string str and returns it
|
||||
* @param str String to be reversed
|
||||
* @return Reversed string
|
||||
*/
|
||||
public static String reverse(String str){
|
||||
if(str.isEmpty() || str == null) return str;
|
||||
|
||||
char arr[] = str.toCharArray();
|
||||
for(int i = 0, j = str.length() - 1; i < j; i++, j--){
|
||||
char temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
return new String(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
* @throws IOException Exception thrown because of BufferedReader
|
||||
*/
|
||||
public static void main(String args[]) throws IOException
|
||||
{
|
||||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter the string");
|
||||
String srr=br.readLine();
|
||||
System.out.println("Reverse="+reverseString(srr));
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
|
||||
26
Misc/countwords.java
Normal file
26
Misc/countwords.java
Normal file
@@ -0,0 +1,26 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* You enter a string into this program, and it will return how
|
||||
* many words were in that particular string
|
||||
*
|
||||
* @author Marcus
|
||||
*
|
||||
*/
|
||||
class CountTheWords{
|
||||
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.println("Enter your text: ");
|
||||
String str = input.nextLine();
|
||||
|
||||
System.out.println("Your text has " + wordCount(str) + " word(s)");
|
||||
input.close();
|
||||
}
|
||||
|
||||
public static int wordCount(String s){
|
||||
if(s.isEmpty() || s == null) return -1;
|
||||
return s.trim().split("[\\s]+").length;
|
||||
}
|
||||
|
||||
}
|
||||
17
Misc/ft.java
Normal file
17
Misc/ft.java
Normal file
@@ -0,0 +1,17 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
class FloydTriangle {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
|
||||
int r = sc.nextInt(), n = 0;
|
||||
|
||||
for(int i=0; i < r; i++) {
|
||||
for(int j=0; j <= i; j++) {
|
||||
System.out.print(++n + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Misc/krishnamurthy.java
Normal file
29
Misc/krishnamurthy.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.util.Scanner;
|
||||
class krishnamurthy
|
||||
{
|
||||
int fact(int n)
|
||||
{
|
||||
int i,p=1;
|
||||
for(i=n;i>=1;i--)
|
||||
p=p*i;
|
||||
return p;
|
||||
}
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int a,b,s=0;
|
||||
System.out.print("Enter the number : ");
|
||||
a=sc.nextInt();
|
||||
int n=a;
|
||||
while(a>0)
|
||||
{
|
||||
b=a%10;
|
||||
s=s+fact(b);
|
||||
a=a/10;
|
||||
}
|
||||
if(s==n)
|
||||
System.out.print(n+" is a krishnamurthy number");
|
||||
else
|
||||
System.out.print(n+" is not a krishnamurthy number");
|
||||
}
|
||||
}
|
||||
45
Misc/removeDuplicateFromString.java
Normal file
45
Misc/removeDuplicateFromString.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*
|
||||
*/
|
||||
|
||||
public class removeDuplicateFromString {
|
||||
public static void main (String[] args) throws Exception{
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
String inp_str = br.readLine();
|
||||
|
||||
System.out.println("Actual string is: " + inp_str);
|
||||
System.out.println("String after removing duplicates: " + removeDuplicate(inp_str));
|
||||
|
||||
br.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method produces a string after removing all the duplicate characters from input string and returns it
|
||||
* Example: Input String - "aabbbccccddddd"
|
||||
* Output String - "abcd"
|
||||
* @param s String from which duplicate characters have to be removed
|
||||
* @return string with only unique characters
|
||||
*/
|
||||
|
||||
public static String removeDuplicate(String s) {
|
||||
if(s.isEmpty() || s == null) {
|
||||
return s;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
int n = s.length();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (sb.toString().indexOf(s.charAt(i)) == -1) {
|
||||
sb.append(String.valueOf(s.charAt(i)));
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user