Leetcode 267. Palindrome Permutation II

在这里插入图片描述
方法1: backtracking。这道题最一开始的想法就是backtracking,但是会tle,经过我小小的优化以后只有一个test case tle了,而且我的算法不能避免duplicate,这些都是问题。虽然没有accepted,但是还是展示一下,至少逻辑是没问题的。

class Solution {
    public List<String> generatePalindromes(String s) {
        Set<String> res = new HashSet<>();
        StringBuilder path = new StringBuilder();
        backtracking(s, 0, path, res);
        return new ArrayList<>(res);
    }
    
    public void backtracking(String s, int start, StringBuilder path, Set<String> res){
        if(start == s.length()){
            StringBuilder ans = new StringBuilder(path);
            if(isPalindrome(ans.toString())) res.add(ans.toString());
            return;
        }
       boolean flag = false;
        for(int i = 0; i <= path.length(); i++){ 
            if(flag) continue;
            if(i != path.length() && s.charAt(start) == path.charAt(i)) flag = true;
            path.insert(i, s.charAt(start));
            backtracking(s, start+1, path, res);
            path.deleteCharAt(i);
        }
    }
    
    public boolean isPalindrome(String s){
        if(s.length() == 0 ||s.length() == 1) return true;
        int left = 0;
        int right = s.length()-1;
        while(left < right){
            if(s.charAt(left) != s.charAt(right)) return false;
            left++;
            right--;
        }
        return true;
    }
}

方法2: 先写一个函数判断这个string能不能构成回文数,如果不行的直接返回空。然后后面还有一个比较tricky的优化是,因为是可以构成回文的,所以我们只选用一半的元素,排列组合出这一半的元素所有可能的permutation,然后后半段直接对称复制上去就行了。时间复杂(n/2)!,空间复杂n/2。详细方法请见lc官方解答2.

public class Solution {
    Set < String > set = new HashSet < > ();
    public List < String > generatePalindromes(String s) {
        int[] map = new int[128];
        char[] st = new char[s.length() / 2];
        if (!canPermutePalindrome(s, map))
            return new ArrayList < > ();
        char ch = 0;
        int k = 0;
        for (int i = 0; i < map.length; i++) {
            if (map[i] % 2 == 1)
                ch = (char) i;
            for (int j = 0; j < map[i] / 2; j++) {
                st[k++] = (char) i;
            }
        }
        permute(st, 0, ch);
        return new ArrayList < String > (set);
    }
    public boolean canPermutePalindrome(String s, int[] map) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i)]++;
            if (map[s.charAt(i)] % 2 == 0)
                count--;
            else
                count++;
        }
        return count <= 1;
    }
    public void swap(char[] s, int i, int j) {
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
    void permute(char[] s, int l, char ch) {
        if (l == s.length) {
            set.add(new String(s) + (ch == 0 ? "" : ch) + new StringBuffer(new String(s)).reverse());
        } else {
            for (int i = l; i < s.length; i++) {
                if (s[l] != s[i] || l == i) {
                    swap(s, l, i);
                    permute(s, l + 1, ch);
                    swap(s, l, i);
                }
            }
        }
    }
}


总结:

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页