Palindrome Partitioning II

来源:互联网 发布:梅雨知时节的作品 编辑:程序博客网 时间:2024/05/29 17:04


public class Solution {    public int minCut(String s) {        if (s == null || s.length() == 0) {            return 0;        }                int length = s.length();                boolean[][] isPalindrome = new boolean[length][length];        int[] cut = new int[length];                for (int j = 0; j < length; j++) {            cut[j] = j;                        for (int i = 0; i <= j; i++) {                if (s.charAt(i) == s.charAt(j) && (j - i <= 1 || isPalindrome[i + 1][j - 1])) {                    isPalindrome[i][j] = true;                                        if (i == 0) {                        cut[j] = 0;                    } else {                        cut[j] = Math.min(cut[j], cut[i - 1] + 1);                    }                }            }        }                return cut[length - 1];    }}


0 0
原创粉丝点击