Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7 especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Single return statements are overrated. There's no practical advantage to forcing your code to have just one return for its own sake.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Single return statements are overrated. There's no practical advantage to forcing your code to have just one return for its own sake.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Single return statements are overrated. There's no practical advantage to forcing your code to have just one return for its own sake.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}
added 140 characters in body
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Single return statements are overrated. There's no practical advantage to forcing your code to have just one return for its own sake.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Single return statements are overrated. There's no practical advantage to forcing your code to have just one return for its own sake.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

For all practical purposes, recursion is a bad idea for this problem, mainly because String.substring() is an expensive operation, especially since Java 7. Let's assume that you're doing this just as a fun educational exercise.

Your base cases are poorly chosen. A zero-length string will cause a crash. If you handle empty strings as a base case, then you no longer need to treat two-character strings as a special case.

You take the string length often enough that it's worth assigning it to a variable.

Overall, the function could be written more compactly.

public static boolean isPalindrome(String s) {
 int len = s.length();
 if (len <= 1) {
 return true;
 }
 return (s.charAt(0) == s.charAt(len - 1)) &&
 isPalindrome(s.substring(1, len - 1));
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /