Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user