zoukankan      html  css  js  c++  java
  • 132. Palindrome Partitioning II(js)

    132. Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome.

    Return the minimum cuts needed for a palindrome partitioning of s.

    Example:

    Input: "aab"
    Output: 1
    Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
    题意:给定一个字符串,问最少切割多少次使得每个子串都是回文
    代码如下:
    /**
     * @param {string} s
     * @return {number}
     */
    var minCut = function(s) {
        let arr=s.split('');
        let n=s.length;
        let pal=[];
        let cut=[];
        for(let i=0;i<n;i++){
            cut[i]=0;
        }
        for(let i=0;i<n;i++){
            pal[i]=[]
            for(let j=0;j<n;j++){
                pal[i][j]=false;
            }
        }
        
        for(let i=0;i<n;i++){
            let min=i;
            for(let j=0;j<=i;j++){
                if((arr[i]===arr[j]) && (j+1>i-1 || pal[j+1][i-1])){
                    pal[j][i]=true;
                    min=j===0?0:Math.min(min,cut[j-1]+1);
                }
            }
            cut[i]=min;
        }
        return cut[n-1];
    };
  • 相关阅读:
    2月3日
    照片测试
    家属签证计时
    我来了
    090204 阴天
    重要提醒to 小爱
    小毛小毛
    C++Primer学习日程
    资料库字段存储文件记录的方式
    本日有点忙
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/10928228.html
Copyright © 2011-2022 走看看