首页 > 其他 > 详细

[LeetCode] Palindrome Permutation

时间:2015-08-21 19:18:58      阅读:491      评论:0      收藏:0      [点我收藏+]

Just check there are no more than 2 characters that appear an odd number of times in the string.

My C++ code using an array as a hash map is as follows.

1 class Solution {
2 public:
3     bool canPermutePalindrome(string s) {
4         int odd = 0, counts[256] = {0};
5         for (char c : s)
6             odd += ++counts[c] & 1 ? 1 : -1;
7         return odd <= 1;
8     }
9 };

BTW, Stefan has posted many nice solutions here, including the following one that uses bitset.

1 class Solution {
2 public:
3     bool canPermutePalindrome(string s) {
4         bitset<256> b;
5         for (char c : s) b.flip(c);
6         return b.count() < 2;
7     }
8 };

 

[LeetCode] Palindrome Permutation

原文:http://www.cnblogs.com/jcliBlogger/p/4748554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!