• 266. Palindrome Permutation


    题目:

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

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

    Hint:

      1. Consider the palindromes of odd vs even length. What difference do you notice?
      2. Count the frequency of each character.
      3. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

    链接: http://leetcode.com/problems/palindrome-permutation/

    3/5/2017

    要学会几种遍历hashmap的方法。

     1 public class Solution {
     2     public boolean canPermutePalindrome(String s) {
     3         HashMap<Character, Integer> h = new HashMap<Character, Integer>();
     4         
     5         for(int i = 0; i < s.length(); i++) {
     6             if (h.containsKey(s.charAt(i))) h.put(s.charAt(i), h.get(s.charAt(i)) + 1);
     7             else h.put(s.charAt(i), 1);
     8         }
     9         int oddCount = 0;
    10         boolean isEvenLength = (s.length() % 2 == 0);
    11         for (HashMap.Entry<Character, Integer> entry: h.entrySet()) {
    12             if (entry.getValue() % 2 != 0) {
    13                 if (isEvenLength) return false;
    14                 else oddCount++;
    15             }
    16         }
    17         return oddCount <= 1;
    18     }
    19 }
  • 相关阅读:
    struts 提交问题
    struts spring整合出错
    hibernate.cfg.xml
    myeclipse copy问题
    myeclipse copy时出的问题
    mysql sql 语句
    Spring_Hibernate
    WebView
    Notification
    Handler(消息机制)
  • 原文地址:https://www.cnblogs.com/panini/p/6507975.html
Copyright © 2020-2023  润新知