Java实现 LeetCode 131 分割回文串

class Go{
    List> result = new ArrayList<>();
    List list = new ArrayList<>();
    char[] arr;
    String s;
    void core(int index){
        if (index == arr.length){
            result.add(new ArrayList<>(list));
            return;
        }
        for (int i = index; i < arr.length; i++){
            if (ishw(index,i)){
                list.add(s.substring(index,i + 1));
                core(i + 1);
                list.remove(list.size() - 1);
            }
        }
    }
    boolean ishw(int left,int right){
        int L = left,R = right;
        while (L <= R){
            if (arr[L++] != arr[R--]){
                return false;
            }
        }
        return true;
    }
    public List> partition(String s) {
        arr = s.toCharArray();
        this.s = s;
        core(0);
        return result;
    }
}
public class Cutpalindrome {
    public static void main(String[] args) {
        Go g = new Go();
        System.out.println(g.partition("aacb"));
    }
}

你可能感兴趣的:(算法练习题,Java,LeetCode,131,分割回文串)