Added functions to check for palindromes and convert nums to bin strings

This commit is contained in:
2021-06-29 13:55:21 -04:00
parent 42b4d4a69d
commit f4e8a6a0f0
2 changed files with 93 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
//JavaClasses/src/main/java/mattrixwv/Algorithms.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 06-01-21
//Modified: 06-29-21
//This class holds many algorithms that I have found it useful to keep around
//As such all of the functions in here are static and meant to be used as stand alone functions
/*
@@ -945,4 +945,27 @@ public class Algorithms{
public static long findNumOccurrence(String str, char c){
return str.chars().filter(ch -> ch == c).count();
}
//Returns true if the string passed in is a palindrome
public static boolean isPalindrome(String str){
String rev = new StringBuilder(str).reverse().toString();
if(str.equals(rev)){
return true;
}
else{
return false;
}
}
//Converts a number to its binary equivalent
public static String toBin(int num){
//Convert the number to a binary string
return Integer.toBinaryString(num);
}
public static String toBin(long num){
//Convert the number to binary string
return Long.toBinaryString(num);
}
public static String toBin(BigInteger num){
//Conver the number to binary string
return num.toString(2);
}
}