TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- public class Palindrome {
-
-
- /**
- * use recursive
- * @param str
- * @return
- */
- static boolean isPalindrome(String str){
- int length = str.length();
- if(length <= 1 )
- return true;
- else if(str.charAt(length -1) == str.charAt(0))
- return isPalindrome(str.substring(1, length - 1));
- else
- return false;
-
- }
-
- /**
- * use for loop
- * @param str
- * @return
- */
- static boolean isPalindrome2(String str){
- char[] ch = str.toCharArray();
- int length = ch.length;
- boolean flag = true;
- for(int i = 0; i < length /2; i++){
- if( ch[i] != ch[length - 1- i]){
- flag = false;
- break;
- }
- }
- return flag;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String str = "aabbyuuybbaa";
- System.out.println(isPalindrome(str));
- System.out.println(isPalindrome2(str));
-
-
- }
-
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/13/000602265.zip |
|