您的位置 首页 java

LeetCode-131-分割回文串

分割回文串

解法一: 递归法

首先处理两种特殊情况,如果字符串为null,直接返回空结果集;如果字符串的长度为1,则只有一种分割情况,直接返回这种情况。

当字符串的长度大于1时,使用递归的方式处理,其中会使用一个判断字符串是否是回文串的方法isHuiwen,递归过程如下:

  • 从字符串的第一个字符开始判断,参数有前面已经被分区的回文串list、当前位置、当前要判断的子串;
  • 首先判断如果已经处理到字符串的最后一个字符,如果当前分区字符串是回文串,则将当前分区字符串添加到partitions,然后将之添加到结果集中,否则,直接返回;
  • 否则,首先判断当前分区字符串是否是回文串,有两种可能:如果是,则将当前分区字符串添加到partitions,将下一个字符作为新的分区字符串开始递归判断;如果不是,将下一个字符添加到当前分区字符串中,递归判断。

最后,返回结果集。

 import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LeetCode_131 {
    // 结果集
     private  static List<List<String>> result = new ArrayList<>();

    public static List<List<String>>  partition (String s) {
        // 如果字符串为null,直接返回空结果集
        if (s == null) {
            return new ArrayList<>();
        }
        // 如果字符串只有一个字符,只可能有一个结果,直接返回
        if (s.length() == 1) {
            List<String> partition = new ArrayList<>();
            partition.add(s);
            result.add(partition);
            return result;
        }

        partition(s, 0, new ArrayList<>(), s.substring(0, 1));
        return result;
    }

    /**
     * 递归方法
     *
     * @param s            原始字符串
     * @param pos          当前位置
     * @param partitions   当前位置之前已经被分割的回文串
     * @param curPartition 当前分区字符串
     */
    private static  void  partition(String s, int pos, List<String> partitions, String curPartition) {
        // 已经处理到字符串的最后一个字符
        if (pos == s.length() - 1) {
            if (isHuiwen(curPartition)) {
                // 如果当前分区字符串是回文串,则将当前分区字符串添加到partitions,然后将之添加到结果集中
                List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
                Collections. copy (newPartitions, partitions);
                newPartitions.add(curPartition);
                result.add(newPartitions);
            }
            return;
        }

        // 如果当前分区字符串是回文串,则将当前分区字符串添加到partitions,然后递归判断下一个字符
        if (isHuiwen(curPartition)) {
            List<String> newPartitions = new ArrayList<>(Arrays.asList(new String[partitions.size()]));
            Collections.copy(newPartitions, partitions);
            newPartitions.add(curPartition);
            partition(s, pos + 1, newPartitions, s.substring(pos + 1, pos + 2));
        }

        // 递归处理下一个字符串
        partition(s, pos + 1, partitions, curPartition + s.substring(pos + 1, pos + 2));
    }

    /**
     * 判断字符串是否是回文串
     *
     * @param str 字符串
     * @return
     */
    private static boolean isHuiwen(String str) {
        if (str == null || str.length() < 2) {
            return true;
        }
        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        for (List<String> string : partition("aab")) {
            System.out.println(string);
        }
    }
}  

【每日寄语】 弃燕雀之小志,慕鸿鹄而高翔。

文章来源:智云一二三科技

文章标题:LeetCode-131-分割回文串

文章地址:https://www.zhihuclub.com/177062.shtml

关于作者: 智云科技

热门文章

评论已关闭

3条评论

  1. Because IBC affects so much of the breast and skin, breast conserving surgery partial mastectomy or lumpectomy and skin sparing mastectomy are not options

  2. Clindamycin was discontinued, and the patient was treated with levofloxacin for the community associated pneumonia, along with steroids, inhalers, and nebulizers for her COPD

网站地图