diff options
| author | pants <niklasjo2004@gmail.com> | 2026-09-21 14:16:34 -0700 |
|---|---|---|
| committer | pants <niklasjo2004@gmail.com> | 2026-09-21 14:16:34 -0700 |
| commit | 81ba9eb66f8284d8760012973f84cdbe617ceb94 (patch) | |
| tree | 466432644e9e3fd66ee183ec53a1f10abb1b7faa /in_class/cse123-inclass9/IsPalindromeArray.java | |
Diffstat (limited to 'in_class/cse123-inclass9/IsPalindromeArray.java')
| -rw-r--r-- | in_class/cse123-inclass9/IsPalindromeArray.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/in_class/cse123-inclass9/IsPalindromeArray.java b/in_class/cse123-inclass9/IsPalindromeArray.java new file mode 100644 index 0000000..109194d --- /dev/null +++ b/in_class/cse123-inclass9/IsPalindromeArray.java @@ -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); + } + } +} |
