亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[Leetcode] Palindrome Permutation 回文變換

svtter / 3084人閱讀

摘要:最笨的方法就是用的解法,找出所有的,然后再用中判斷回文的方法來判斷結(jié)果中是否有回文。而中心對稱點(diǎn)如果是字符,該字符會是奇數(shù)次,如果在兩個字符之間,則所有字符都是出現(xiàn)偶數(shù)次。

Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome.

For example, "code" -> False, "aab" -> True, "carerac" -> True.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice? Count the frequency of each character. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

哈希表法 復(fù)雜度

時間 O(N) 空間 O(N)

思路

Leetcode的提示其實已經(jīng)將答案告訴我們了。最笨的方法就是用Permutations的解法,找出所有的Permutation,然后再用Palindrome中判斷回文的方法來判斷結(jié)果中是否有回文。但是我們考察一下回文的性質(zhì),回文中除了中心對稱點(diǎn)的字符,其他字符都會出現(xiàn)偶數(shù)次。而中心對稱點(diǎn)如果是字符,該字符會是奇數(shù)次,如果在兩個字符之間,則所有字符都是出現(xiàn)偶數(shù)次。所以,我們只要判斷下字符串中每個字符出現(xiàn)的次數(shù),就知道該字符串的其他排列方式中是否有回文了。

注意

本題也可以用一個HashSet,第偶數(shù)個字符可以抵消Set中的字符,最后判斷Set的大小是否小于等于1就行了。

代碼

HashMap實現(xiàn)

public class Solution {
    public boolean canPermutePalindrome(String s) {
        Map map = new HashMap();
        // 統(tǒng)計每個字符的個數(shù)
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            Integer cnt = map.get(c);
            if(cnt == null){
                cnt = new Integer(0);
            }
            map.put(c, ++cnt);
        }
        // 判斷是否只有不大于一個的奇數(shù)次字符
        boolean hasOdd = false;
        for(Character c : map.keySet()){
            if(map.get(c) % 2 == 1){
                if(!hasOdd){
                    hasOdd = true;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

HashSet實現(xiàn)

public class Solution {
    public boolean canPermutePalindrome(String s) {
        Set set = new HashSet();
        for(int i = 0; i < s.length(); i++){
            // 出現(xiàn)的第偶數(shù)次,將其從Set中移出
            if(set.contains(s.charAt(i))){
                set.remove(s.charAt(i));
            } else {
            // 出現(xiàn)的第奇數(shù)次,將其加入Set中
                set.add(s.charAt(i));
            }
        }
        // 最多只能有一個奇數(shù)次字符
        return set.size() <= 1;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/64591.html

相關(guān)文章

  • LeetCode 精選TOP面試題【51 ~ 100】

    摘要:有效三角形的個數(shù)雙指針最暴力的方法應(yīng)該是三重循環(huán)枚舉三個數(shù)字??偨Y(jié)本題和三數(shù)之和很像,都是三個數(shù)加和為某一個值。所以我們可以使用歸并排序來解決這個問題。注意因為歸并排序需要遞歸,所以空間復(fù)雜度為 ...

    Clect 評論0 收藏0
  • [LeetCode] 266. Palindrome Permutation

    Problem Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: codeOutput: falseExample 2: Input: aabOutput: trueExample 3: Input: careracOutput: true Solu...

    jlanglang 評論0 收藏0
  • [LeetCode] 267. Palindrome Permutation II

    Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...

    huashiou 評論0 收藏0
  • [Leetcode] Palindrome Number 回文數(shù)

    摘要:反轉(zhuǎn)比較法復(fù)雜度時間空間思路回文數(shù)有一個特性,就是它反轉(zhuǎn)后值是一樣的。代碼逐位比較法復(fù)雜度時間空間思路反轉(zhuǎn)比較有可能會溢出,但我們遍歷每一位的時候其實并不用保存上一位的信息,只要和當(dāng)前對應(yīng)位相等就行了。首先,負(fù)數(shù)是否算回文。 Palindrome Number Determine whether an integer is a palindrome. Do this witho...

    _Suqin 評論0 收藏0
  • LeetCode 336. Palindrome Pairs

    摘要:描述給定一組唯一的單詞,找出所有不同的索引對,使得列表中的兩個單詞,,可拼接成回文串。遍歷每一個單詞,對每一個單詞進(jìn)行切片,組成和。 Description Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenatio...

    TigerChain 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<