码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome Permutation -- LeetCode

时间:2016-08-15 06:41:03      阅读:119      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!