LeetCode-面试题01.04:回文排列

一、题目描述

在这里插入图片描述

二、解题思路

就是用一个Hash表存储字符串中出现过的字符,待字符串遍历完成后,再遍历一遍Hash表,如果出现次数为奇数的字母超过1个,就不可能重组为回文串,否则就可以重组为回文串。

三、解题代码

class Solution {
public:
    bool canPermutePalindrome(string s) {
        vector<int> Hash(128, 0);
        unsigned int how_much_single = 0;
        for(size_t i = 0; i < s.size(); i++)    Hash[s[i]]++;
        for(auto it: Hash)
            if(it % 2)    how_much_single++;
        return how_much_single <= 1;
    }
};

四、运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44587168/article/details/105974036