首页 > 其他 > 详细

5:Longest Palindromic Substring

时间:2015-07-11 08:56:58      阅读:250      评论:0      收藏:0      [点我收藏+]

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

 

public String longestPalindrome(String s) {
        
        int tempLen = 0;
        String returnStr = null;
        for(int i=0;i<s.length()-1;i++){
            for (int j=i+1;j<s.length();j++){
                String tempStr = s.substring(i, j);
                if(isPalindromicString(tempStr)){
                    if(tempStr.length()>tempLen){
                        tempLen = tempStr.length();
                        returnStr = tempStr;
                    }
                    
                }
            }
        }
        System.out.println(tempLen);
        return returnStr;
    }
    
    /**
     * 判断一个字符串是否为回文
     * @param str
     * @return
     */
    private boolean isPalindromicString(String str){
        int len = str.length(),len2=str.length()/2;
        len2 = len%2==1?len2+1:len2;
        for(int i=0;i<len2;i++){
            if(str.charAt(i)!=str.charAt(len-1-i)){
                return false;
            }
        }
        return true;
    }
   

 

5:Longest Palindromic Substring

原文:http://www.cnblogs.com/shisw/p/4637985.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!