zoukankan      html  css  js  c++  java
  • LeetCode 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.


    题目标签:Hash Table

      题目给了我们一个string s,让我们判断它是不是可以变作回文。

      看一下回文特性:

        如果是偶数长度的回文,其中所有的char 都是偶数出现次数;

        如果是奇数长度的回文,那么只可以有一个char 是奇数的出现次数。

      

      只要把char 当作key,它的出现次数当作value 存入HashMap,之后遍历keyset 来验证出现次数就可以了。

    Java Solution:

    Runtime beats 29.15% 

    完成日期:11/05/2017

    关键词:HashMap

    关键点:char 当作 key,出现次数当作 value 存入

     1 class Solution 
     2 {
     3     public boolean canPermutePalindrome(String s) 
     4     {
     5         HashMap<Character, Integer> map = new HashMap<>();
     6         int oddChar = 0;
     7         
     8         if(s.length() % 2 != 0)
     9             oddChar = 1;
    10         
    11         for(char c: s.toCharArray())
    12             map.put(c, map.getOrDefault(c, 0) + 1);
    13             
    14 
    15         for(char c: map.keySet())
    16         {
    17             if(oddChar < 0)
    18                 return false;
    19             
    20             if(map.get(c) % 2 != 0)
    21                 oddChar--;
    22         }
    23         
    24         
    25         return true;
    26     }
    27 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

  • 相关阅读:
    2012航拍香港
    2012航拍香港
    论玩镜头的三种境界[转自无忌fruitbear]
    论玩镜头的三种境界[转自无忌fruitbear]
    认识镜头的MTF值
    认识镜头的MTF值
    宾得十大名镜
    宾得十大名镜
    两个输入通道怎么判断通道顺序
    增加新功能和未知的修改操作
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7791468.html
Copyright © 2011-2022 走看看