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 存入
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 (回文排列)$的更多相关文章
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode Valid Palindrome 有效回文(字符串)
class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Android集成微信分享功能应用签名生成方法及分享不生效的问题
通过友盟sdk集成微博.微信.qq等分享功能时,微博和qq很顺利,但在做微信集成时一直不成功.主要问题还是之前在微信开放平台申请创建移动应用时,对应用签名没有填写对,走了很多弯路现总结出来,加深记忆避 ...
- Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建;学习Python语言,利用Python语言来写测试用例。加油!!!
Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建:学习Python语言,利用Python语言来写测试用例.加油!!!
- ProcessBar 与SeekBar进度条
1.进度条关键属性 2.进度条的常用方法 progress = (ProgressBar) findViewById(R.id.horiz); (1)获取第一进度条:progress.getProgr ...
- Windows Server 2008不能Ping改为允许的方法
用了Windows Server 2008朋友肯定都知道,2008在很多设置方面与2003不同,尤其在安全上进行了加强,例如:默认情况下Windows 2008是不允许PING的,那么如何打开允许PI ...
- RabbitMQ - Publisher的消息确认机制
queue和consumer之间的消息确认机制:通过设置ack.那么Publisher能不到知道他post的Message有没有到达queue,甚至更近一步,是否被某个Consumer处理呢?毕竟对于 ...
- facade模式-服务-配置----系统生成与配置
facade模式对外提供一组相关服务: 对内整合子系统: facade模式的创建过程需要依赖外部的配置. 配置完成以后才能使用服务. 推广开来,所有系统都需要生成与配置,然后才能对外提供服务.
- eclipse配置Tomcat服务器server locations的方法
最近放弃了使用Myeclipse,转而使用eclipse作为开发工具,确实Myeclipse集成了太多东西,使得开发人员的配置越来越少,这不是个好事,使用eclipse后,有些地方就得自己去配置,比如 ...
- boostrap标签
字体: <lead>:加强显示 <strong><b>:字体加粗 <i><em>:斜体字 .text-muted:提示,使用浅灰色(#999 ...
- 诊断:expdp导出时遇到错误ORA-31693和ORA-00922
11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...
- 手写redis的docker文件,通过docker-compose配置redis
在前面一遍随笔,配置的是mysql主从的docker-compose配置.今天我们来学习配置编排容器redis. 准备环境: docker 18.06.1-ce docker-compose 1.23 ...