leetcode题目原链接:. - 力扣(LeetCode)
思路:简单哈希数组放入,然后判断哈希值,写例子可知道如果哈希值即对应字母出现的个数为奇数, 如果这个奇数大于1就一定false,而偶数有无几个均不影响
bool canPermutePalindrome(char* s) {
int len = strlen(s);
int max = s[0];
int min = s[0];
// write code here
for (int i = 0; s[i] != '\0'; i++) {
if (max <= s[i]) {
max = s[i];
}
if (min >= s[i]) {
min = s[i];
}
}
int* p = (int*)calloc(max - min + 1, sizeof(int));
for (int i = 0; s[i] != '\0'; i++) {
p[s[i] - min]++;
}
int odd = 0;
int even = 0;
for (int i = 0; i < max - min + 1; i++) {
if (p[i] % 2 != 0) {
odd++;
}
}
if (odd <= 1) {
return true;
}
else {
return false;
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有