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 存入

 class Solution
{
public boolean canPermutePalindrome(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
int oddChar = 0; if(s.length() % 2 != 0)
oddChar = 1; for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); for(char c: map.keySet())
{
if(oddChar < 0)
return false; if(map.get(c) % 2 != 0)
oddChar--;
} return true;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 266. Palindrome Permutation (回文排列)$的更多相关文章

  1. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  2. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

  3. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  5. [LeetCode] Palindrome Permutation 回文全排列

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

  6. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  8. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  9. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. Mysql慢SQL与索引案例

    写在最前 关于慢sql的开启与配置查看之前我整理的文章: http://www.cnblogs.com/hanxiaobei/p/5515624.html 前提准备: tomcat7.x mysql- ...

  2. HP M177打印机驱动安装问题与解决

    问题描述 采购HP Color LaserJet Pro MFP M177 PCLmS一台,采用局域网WIFI网络与办公室各电脑连接.安装打印机自带光盘驱动,发现有些电脑安装驱动失败,提示“出现严重错 ...

  3. LDA算法(入门篇)

    一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discriminant ,FLD ...

  4. PHP7 上传文件报错 Internal Server Error 解决方法

    打开Apache配置httpd.conf.在最后添加FcgidMaxRequestLen指令一个足够大的值(以字节为单位),例如 FcgidMaxRequestLen 100000000 最后重新启动 ...

  5. putty源码阅读----plink

    一直对ssh协议的各种客户端实现比较入迷,遍寻了很多ssh协议实现也用了很多的库,发现依赖太多 putty是最纯洁依赖第三方几乎为0的客户端实现,先从plink处开始入手. 1.putty目录 才刚开 ...

  6. HDU_1166_敌兵布阵

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. arx代码片段

    ObjectARX代码片段二   转载自网络 一  在ARX中禁用AutoCAD的某个命令 以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令: acedCommand(RTSTR, &q ...

  8. 梦想CAD控件安卓参数绘图

    在CAD绘图中,参数化绘图可以帮助我们极大缩短设计时间,用户可以按照设计意图控制绘图对象,这样即使对象发生了变化,具体的关系和测量数据仍将保持不变,能够对几何图形和标注进行控制,可以帮助用户应对耗时的 ...

  9. nginx_rewrite规则介绍

    rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且只能 ...

  10. 01JavaScript使用

    JavaScript使用 1.内容写入 HTML <P onmouseover="alert('欢迎您学习JavaScript!')">鼠标移过来</P> ...