1. 程式人生 > >java 求最長迴文子串

java 求最長迴文子串

/**
     * 求最長迴文子串
     * 子串:連續的
     * 暴力窮舉
     */
    public static String get01() {
        String str = "googlepppe";
        int length = str.length();
        String finalStr = "";
        for (int start = 0; start < length; start++) {
            for (int end = start + 1; end <= length; end++) {
                String temp = str.substring(start, end);
                if (temp != null) {
                    if (temp.length() > finalStr.length() && isMirror(temp)) {
                        finalStr = temp;
                    }
                }
            }
        }
        Log.i("test","finalStr="+finalStr);
        return finalStr;
    }

    public static boolean isMirror(String str){
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i) != str.charAt(str.length()-i-1)){
                return false;
            }
        }
        return true;
    }
}

方法比較傻,還有別的方法,待續