LeetCode 267. Palindrome Permutation II(对称排列)

28 篇文章 0 订阅
20 篇文章 0 订阅

原题网址:https://leetcode.com/problems/palindrome-permutation-ii/

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.

For example:

Given s = "aabb", return ["abba", "baab"].

Given s = "abc", return [].

Hint:

  1. If a palindromic permutation exists, we just need to generate the first half of the string.
  2. To generate all distinct permutations of a (half of) string, use a similar approach from: Permutations II or Next Permutation.

方法:深度优先搜索,生成前半段,后半段镜像复制。

public class Solution {
    private void find(char[] chs, char center, int left, int right, char[] palindrome, List<String> results) {
        if (left == right) {
            palindrome[left] = center;
            results.add(new String(palindrome));
            return;
        } else if (left > right) {
            results.add(new String(palindrome));
            return;
        }
        boolean[] used = new boolean[256];
        for(int i=left; i<chs.length; i++) {
            if (used[chs[i]]) continue;
            used[chs[i]] = true;
            char t = chs[left];
            chs[left] = chs[i];
            chs[i] = t;
            palindrome[left] = chs[left];
            palindrome[right] = chs[left];
            find(chs, center, left+1, right-1, palindrome, results);
            t = chs[left];
            chs[left] = chs[i];
            chs[i] = t;
        }
    }
    public List<String> generatePalindromes(String s) {
        List<String> results = new ArrayList<>();
        char[] sa = s.toCharArray();
        int[] f = new int[256];
        for(int i=0; i<sa.length; i++) f[sa[i]] ++;
        char center = ' ';
        boolean single = false;
        int count = 0;
        for(int i=0; i<f.length; i++) {
            if ((f[i] & 1) == 1) {
                if (single) return results;
                single = true;
                center = (char)i;
                f[i] --;
            }
            count += f[i];
        }
        if ((sa.length & 1) == 0 && single) return results;
        if ((sa.length & 1) == 1 && !single) return results;
        char[] chs = new char[count/2];
        int size = 0;
        for(int i=0; i<f.length; i++) {
            for(int j=0; j<f[i]/2; j++) {
                chs[size++] = (char)i;
            }
        }
        find(chs, center, 0, sa.length-1, new char[sa.length], results);
        return results;
    }
}

used数组每次都要创建比较浪费空间和时间,如果每个深度使用字符从0~255进行循环,则不需要担心重复的问题,就可以通过计数器来维护使用状态。

public class Solution {
    private void find(int[] frequency, int[] used, char center, char[] permutation, int step, List<String> results) {
        if (step == permutation.length/2) {
            if ((permutation.length & 1) == 1) permutation[step++] = center;
            for(int i=0, j=permutation.length-1; i<j; i++, j--) permutation[j] = permutation[i];
            results.add(new String(permutation));
            return;
        }
        for(char ch=0; ch<frequency.length; ch++) {
            if (frequency[ch] == 0 || used[ch] >= (frequency[ch]>>1)) continue;
            used[ch] ++;
            permutation[step] = ch;
            find(frequency, used, center, permutation, step+1, results);
            used[ch] --;
        }
    }
    public List<String> generatePalindromes(String s) {
        List<String> results = new ArrayList<>();
        char[] sa = s.toCharArray();
        boolean single = false;
        char center = '\0';
        int[] frequency = new int[256];
        for(int i=0; i<sa.length; i++) frequency[sa[i]] ++;
        for(char i=0; i<frequency.length; i++) {
            if ((frequency[i] & 1) == 1) {
                if (!single) {
                    single = true;
                    center = i;
                } else {
                    return results;
                }
            }
        }
        find(frequency, new int[256], center, new char[sa.length], 0, results);
        return results;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值