LeetCode 05 最长回文子串 暴力破解和动态规划

5. Longest Palindromic Substring    难度:Medium

 

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

 

回文:即正着读和反着读都是一样的字符串

 

刚开始就只想到暴力解法,最直观,找到所有的字符串组合,挨个判断是否为回文,并记录下最长的那一个即可,时间复杂度:O (n三次方)

 

后面看可以用动态规划的方法求解,我自个就在大学实验课上遇到那么两三次动态规划的问题,当时明白了,现在早就忘光了,刚开始感觉一头雾水,隔了两天,又回头来看这个问题,写了一个多小时。。。慢慢写了出来,如下:

package pers.leetcode;

/**
 * LeetCode 05 Longest Palindromic Substring
 * 难易程度: Medium
 *
 * @author admin
 * @date 2019/3/15 10:48
 */
public class LongesetPalindrome {

    public static void main(String[] args) {
        String s = "ac";
        System.out.println("暴力破解: " + longestPalindrome(s));
        System.out.println("动态规划: " + dp(s));
    }

    /**
     * 暴力破解 时间复杂度 O(n三次方)
     */
    public static String longestPalindrome(String s){
        int length = s.length();
        String target = "";
        for (int i=0; i target.length()){
                        target = substring;
                    }
                }
            }
        }
        return target;
    }

    /**
     * 判断一个字符串是否为回文串
     */
    public static boolean palindrome(String s){
        if (s == null){
            return false;
        }
        if (s.length() == 1){
            return true;
        }
        for (int i=0; i=i 的情况 字符串正着来一遍就可以了
     *          行从最后一行开始
     *
     * @param s 字符串
     * @return true/false
     */
    public static String dp(String s){
        int length = s.length();
        if (length == 1){
            return s;
        }

        // 创建一个空数组
        int[][] dy = new int[length][length];

        // 初始化数组值
        for (int i = 0; i < length; i++){
            for (int j = i ; j < length; j++){
                if (i == j){
                    dy[i][j] = 1;
                }else {
                    dy[i][j] = 0;
                }
            }
        }

        // 动态规划
        for (int i = length-1; i >= 0; i--){
            for (int j = i + 1; j < length; j++){
                if (j - i == 1){
                    if (s.charAt(i) == s.charAt(j)){
                        dy[i][j] = 1;
                    }else{
                        dy[i][j] = 0;
                    }
                }else {
                    if (dy[i+1][j-1] == 1 && (s.charAt(i) == s.charAt(j))){
                        dy[i][j] = 1;
                    }else{
                        dy[i][j] = 0;
                    }
                }
            }
        }

        // 设置变量
        String target = "";
        int num = 0;

        // 打印验证回文数组
        for (int i = 0; i < length; i++){
            for (int j = 0; j < length; j++){
                //System.out.printf("%s  ",dy[i][j]);
                if (dy[i][j] == 1){
                    if (j - i >= num){
                        target = s.substring(i,j+1);
                        num = j - i;
                    }
                }
            }
           // System.out.printf("\n");
        }
        return target;
    }

}

 

你可能感兴趣的:(LeetCode)