LeetCode一道题:回文数划分的最小次数

来源:互联网 发布:淘宝咸鱼搜索 编辑:程序博客网 时间:2024/05/16 01:00
public class Sholution {    private boolean isPalindrome(char[] ch, int i,int j){        while(i<j){            if(ch[i]!=ch[j]){                return false;            }            ++i;            --j;        }        return true;    }public int minCut(String s){        int totalLength = s.length();        int[] M = new int[totalLength];        char[] ch = s.toCharArray();        M[0]=0;        for(int i=1;i<totalLength;++i){            int min = 99999;            for(int j=0;j<=i;++j){                int cut = 99999;                if(isPalindrome(ch,j,i)){                    if(j>0){                        cut = M[j-1]+1;                    }                    else{                        cut = 0;                    }                    if(min>cut){                        min = cut;                    }                }            }                        M[i]=min;        }        return M[totalLength-1];    }}