首页 > 其他 > 详细

Palindrome Permutation -- LeetCode

时间:2016-08-15 06:38:02      阅读:107      评论:0      收藏:0      [点我收藏+]

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

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

 

要点:学习如何iterate一个unordered_map。

 1 class Solution {
 2 public:
 3     bool canPermutePalindrome(string s) {
 4         unordered_map<char, int> dict;
 5         for (int i = 0, n = s.size(); i < n; i++) {
 6             if (dict.count(s[i]) == 0)
 7                 dict.insert(make_pair(s[i], 1));
 8             else dict[s[i]]++;
 9         }
10         int oddCount = 0;
11         for (auto it = dict.begin(); it != dict.end(); it++) {
12             if (it->second % 2) oddCount++;
13             if (oddCount > 1) return false;
14         }
15         return true;
16     }
17 };

 

Palindrome Permutation -- LeetCode

原文:http://www.cnblogs.com/fenshen371/p/5771537.html

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