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 ...
随机推荐
- 联想 S5 Pro GT(L78091)免解锁BL 免rec 保数据 ROOT Magisk Xposed 救砖 ZUI5.0.047
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- jsp学习笔记 - 内置对象 pageContext
1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest(); 得到的对象只是属于ServletRequest类,httpServletReques ...
- vps 虚拟服务器 教程 ( Virtual Private Server 虚拟专用服务器 )
VPS是虚拟服务器的意思.他是通过软件在独立服务器上划分出来的一部分资源.从而虚拟出一个服务器.他拥有独立的IP.独立的操作系统.以及用户名和密码.在功能和使用方法上与服务器一模一样.用户也可以根据自 ...
- linux nohup & 简单使用
线上通常nohup配合&启动程序,同时免疫SIGINT和SIGHUP信号,从而保证程序在后台稳定运行 & 1.后台运行,输出默认到屏幕 2.免疫SIGINT信号,比如Ctrl+c不会杀 ...
- 第五届蓝桥杯校内选拔第六题_(dfs)
你一定听说过“数独”游戏.如[图1.png],玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独的答案都是唯一的,所以 ...
- js判断是安卓 还是 ios webview
判断原理:JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来 ...
- 梦想CAD控件安卓选择集
在本示例中将使用构造选择集对被过滤对象进行过滤,该类封装了选择集及其处理函数,支持如下过滤条件. 参数类型 类型 RTDXF0 TEXT 文字 MTEXT 多行文字 CIRCLE 圆 ARC 圆弧 L ...
- C语言输入一行整数(OJ输入格式)
就是说输入一行用空格隔开的函数,可是它没说用回车符结束,所以一定要用EOF了 第一种方法: ; char ch; do { scanf("%ld",&a[++t]); } ...
- 03匿名内部类、eclipse快捷键、String相关知识
03匿名内部类.eclipse快捷键.String相关知识-2018.7.11 1.匿名内部类(只针对重写一个方法时候使用,不能向下转型,因为没有子类类名) new Inter(){ public v ...
- config对象的使用及常用方法
config对象的使用及常用方法 制作人:全心全意 config对象主要用于取得服务器的配置信息.通过pageContext对象的getServletConfig()方法可以获取一个config对象. ...