题目描述
给定一个字符串,编写一个函数判定其是否为某个回文串的排列之一。
回文串是指正反两个方向都一样的单词或短语。排列是指字母的重新排列。
回文串不一定是字典当中的单词。
我的解题
class Solution {
public:
bool canPermutePalindrome(string s) {
int l = s.length();
if(l<=1) return true;
vector<int>record(26,0);
for(auto str: s)
{
record[str -'a']+=1;
}
bool flag = false;
for(int i=0; i<26; i++)
{
if(flag && (record[i]&0x01!=0) ) return false;
if((record[i]&0x01!=0)) flag = true;
}
return true;
}
};
= = 字符不知小写字母
class Solution {
public:
bool canPermutePalindrome(string s) {
set<char>record;
for (auto c : s) {
if( record.find(c) != record.end())
{
record.erase(c);
}
else
{
record.insert(c);
}
}
if(record.size()<=1) return true;
return false;
}
};
执行用时 :4 ms, 在所有 C++ 提交中击败了61.89%的用户
内存消耗 :8.2 MB, 在所有 C++ 提交中击败了100.00%的用户