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. web测试--登录界面怎么测?

    具体需求: 有一个登陆页面, 上面有2个textbox, 一个提交按钮.  请针对这个页面设计30个以上的测试用例. 此题的考察目的: 面试者是否熟悉各种测试方法,是否有丰富的Web测试经验, 是否了 ...

  2. Python+selenium学习(二) selenium 定位不到元素

    转载:https://www.cnblogs.com/tarrying/p/9681991.html tarrying selenium的三种等待时间 //隐式等待(20秒以内没哥一段时间就会去找元素 ...

  3. Caffe RPN:把RPN网络layer添加到caffe基础结构中

    在测试MIT Scene Parsing Benchmark (SceneParse150)使用FCN网络时候,遇到Caffe错误. 遇到错误:不可识别的网络层crop 网络层 CreatorRegi ...

  4. java对于07excel的读、改、写、并触发计算

    InputStream is = null; try { is = new FileInputStream(filePath); } catch (FileNotFoundException e1) ...

  5. ubuntu16.04安装teamviewer12

    安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb   ...

  6. day16-常用模块I(time、datetime、random、os、sys、json、pickle)

    目录 time模块 datetime模块 random模块 os模块 sys模块 json模块与pickle模块 json pickle time模块 time模块一般用于不同时间格式的转换,在使用前 ...

  7. 16Log4J

    Log4J Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX Syslog守 ...

  8. 简单的jsonp实现跨域原理

    什么原因使jsonp诞生?  传说,浏览器有一个很重要的安全限制,叫做"同源策略".同源是指,域名,协议,端口相同.举个例子,用一个浏览器分别打开了百度和谷歌页面,百度页面在执行脚 ...

  9. ie6,ie7,ie8,FF 浏览器兼容问题

    javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.item("itemName") 这样的语句,不能在 ...

  10. 散列(hash)

    散列(hash)是常用的算法思想之一,在很多程序中都会有意无意地使用到. 先来看一个简单的问题:给出N个正整数,再给出M个正整数,问这M个数中每个数分别是否在N个数中出现过. 例如N=5,M=3,N个 ...