最长回文子符串

回文字符串就是正反字符串一样。

  1. public class Main19 {  
  2.     public static void main(String[] args) {  
  3.         System.out.println(getPalindrome("12ABCBA23"));  
  4.     }  
  5.   
  6.     /** 
  7.      * 双指针法获取test最长回文子串 
  8.      * @param test 
  9.      * @return 
  10.      */  
  11.     public static String getPalindrome(String test){  
  12.         int length = test.length();  
  13.         /** 
  14.          * 第1个指针从字符串尾部开始 
  15.          * 第2个指针从字符串头部开始 
  16.          */  
  17.         for(int point01 = length;point01 >= 1;point01--){  
  18.             for(int point02 = 0;point02 + point01 <= length;point02++){  
  19.                 int left = point02;  
  20.                 int right = point02 + point01 -1;  
  21.                 while (left < right && test.charAt(left) == test.charAt(right)){  
  22.                     left++;  
  23.                     right--;  
  24.                 }  
  25.                 if(left >= right){  
  26.                     return test.substring(point02,point01+1);  
  27.                 }  
  28.             }  
  29.   
  30.         }  
  31.         return "";  
  32.     }  

优化后:命名、方法行数、功能细化、抽取

  1. import java.nio.charset.StandardCharsets;  
  2. import java.util.Scanner;  
  3.   
  4. /** 
  5.  *  最长回文子串-优化 
  6.  */  
  7. public class Main19Optimize {  
  8.     public static void main(String[] args) {  
  9.         Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());  
  10.         while (scanner.hasNextLine()){  
  11.             String test = scanner.nextLine();  
  12.             // 输入12ABCBA23输出2ABCBA2  
  13.             System.out.println(getMaxPalindromeChild(test));  
  14.         }  
  15.     }  
  16.   
  17.     /** 
  18.      * 双指针法获取test最长回文子串 
  19.      * @param  test 待测试的字符串 
  20.      * @return 回文字符串,没有返回空 
  21.      */  
  22.     public static String getMaxPalindromeChild(String test){  
  23.         if(null == test || test.length() == 0){  
  24.             return "";  
  25.         }  
  26.         int length = test.length();  
  27.         /** 
  28.          * 第1个指针从字符串尾部开始 
  29.          * 第2个指针从字符串头部开始 
  30.          */  
  31.         for(int rightPoint = length;rightPoint >= 1;rightPoint--){  
  32.             for(int leftPoint = 0;leftPoint + rightPoint <= length;leftPoint++){  
  33.                if(isPalindrome(test,leftPoint,leftPoint + rightPoint -1)){  
  34.                    return test.substring(leftPoint,leftPoint +rightPoint);  
  35.                }  
  36.             }  
  37.         }  
  38.         return "";  
  39.     }  
  40.   
  41.     /** 
  42.      *  判断test指定范围的子串是否是回文 
  43.      * @param test 待测试的子串 
  44.      * @param leftPoint 左指针 
  45.      * @param rightPoint 右指针 
  46.      * @return true为回文,false不是 
  47.      */  
  48.     public static boolean isPalindrome(String test,int leftPoint ,int rightPoint){  
  49.         while (leftPoint < rightPoint && test.charAt(leftPoint) == test.charAt(rightPoint)){  
  50.             leftPoint++;  
  51.             rightPoint--;  
  52.         }  
  53.         return  leftPoint >= rightPoint;  
  54.     }  
  55. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值