init
This commit is contained in:
25
in_class/cse123-inclass9/IsPalindromeArray.java
Normal file
25
in_class/cse123-inclass9/IsPalindromeArray.java
Normal file
@@ -0,0 +1,25 @@
|
||||
public class IsPalindromeArray {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 }));
|
||||
System.out.println(isPalindrome(new int[] { 10, 20, 20, 10 }));
|
||||
System.out.println(isPalindrome(new int[] { 1 }));
|
||||
System.out.println(isPalindrome(new int[] {}));
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5 }));
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 5, 3 }));
|
||||
System.out.println(isPalindrome(new int[] { 10, 20, 30, 10 }));
|
||||
}
|
||||
|
||||
public static boolean isPalindrome(int[] array) {
|
||||
return isPalindrome(array, 0, array.length - 1);
|
||||
}
|
||||
|
||||
private static boolean isPalindrome(int[] array, int lower, int upper) {
|
||||
if (upper - lower < 1) {
|
||||
return true;
|
||||
} else if (array[lower] != array[upper]) {
|
||||
return false;
|
||||
} else {
|
||||
return isPalindrome(array, lower+1,upper-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
in_class/cse123-inclass9/IsPalindromeString.java
Normal file
19
in_class/cse123-inclass9/IsPalindromeString.java
Normal file
@@ -0,0 +1,19 @@
|
||||
public class IsPalindromeString {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isPalindrome("racecar"));
|
||||
System.out.println(isPalindrome("tacocat"));
|
||||
System.out.println(isPalindrome("ABBA"));
|
||||
System.out.println(isPalindrome("x"));
|
||||
System.out.println(isPalindrome(""));
|
||||
System.out.println(isPalindrome("banana"));
|
||||
System.out.println(isPalindrome("abcbba"));
|
||||
System.out.println(isPalindrome("bramb"));
|
||||
}
|
||||
|
||||
public static boolean isPalindrome(String s) {
|
||||
if (s.length() <= 1 || s.equals(ReverseString.reverse(s))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
38
in_class/cse123-inclass9/RecursiveMystery.java
Normal file
38
in_class/cse123-inclass9/RecursiveMystery.java
Normal file
@@ -0,0 +1,38 @@
|
||||
public class RecursiveMystery {
|
||||
public static void main(String[] args) {
|
||||
// mystery1(3);
|
||||
// mystery2(4);
|
||||
mystery3("taco");
|
||||
}
|
||||
|
||||
public static void mystery1(int n) {
|
||||
if (n <= 1) {
|
||||
System.out.print(n);
|
||||
} else {
|
||||
mystery1(n / 2);
|
||||
System.out.print(", " + n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void mystery2(int n) {
|
||||
if (n <= 0) {
|
||||
System.out.print("*");
|
||||
} else if (n % 2 == 0) {
|
||||
System.out.print("(");
|
||||
mystery2(n - 1);
|
||||
System.out.print(")");
|
||||
} else {
|
||||
System.out.print("[");
|
||||
mystery2(n - 1);
|
||||
System.out.print("]");
|
||||
}
|
||||
}
|
||||
|
||||
public static void mystery3(String str) {
|
||||
if (!str.isEmpty()) {
|
||||
System.out.print(str.charAt(0));
|
||||
mystery3(str.substring(1));
|
||||
System.out.print(str.charAt(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
in_class/cse123-inclass9/ReverseString.java
Normal file
22
in_class/cse123-inclass9/ReverseString.java
Normal file
@@ -0,0 +1,22 @@
|
||||
public class ReverseString {
|
||||
public static void main(String[] args){
|
||||
System.out.println(reverse("Hello"));
|
||||
System.out.println(reverse("nathan"));
|
||||
System.out.println(reverse("racecar"));
|
||||
System.out.println(reverse("taco"));
|
||||
System.out.println(reverse("ABBA"));
|
||||
System.out.println(reverse(""));
|
||||
System.out.println(reverse("a"));
|
||||
}
|
||||
|
||||
public static String reverse(String str){
|
||||
if (str.isEmpty()) {
|
||||
return "";
|
||||
} else if (str.length() == 1) {
|
||||
return str;
|
||||
}
|
||||
|
||||
String backwards = reverse(str.substring(1)) + str.charAt(0);
|
||||
return backwards;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user