`

Leetcode - Palindrome Permutation

 
阅读更多
[分析]
思路2让我大开眼界,顺便学习下BitSet~
[ref]
https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java

public class Solution {
    // Method 2: https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java
    public boolean canPermutePalindrome(String s) {
        BitSet bs = new BitSet();
        for (byte b : s.getBytes())
            bs.flip(b);
        return bs.cardinality() < 2;
    }
    // Method 1
    public boolean canPermutePalindrome1(String s) {
        int[] map = new int[256];
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i)]++;
        }
        int oddOccurCounter = 0;
        for (int i = 0; i < 256; i++) {
            if (((map[i]) & 1) == 1)
                oddOccurCounter++;
        }
        if ((s.length() & 1) == 1)
            return oddOccurCounter == 1;
        else
            return oddOccurCounter == 0;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics