【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.
解析
判断是否可以组成回文
给一个字符串,判断字符串中的字符是否可以组成回文
思路
其实思路都一样,计算字符串中的字符个数;若字符串的字符有偶数个,则每个字符的出现次数需要有偶数次;若字符串的字符有奇数个,则最多只能有一个字符出现过奇数次,其他字符都需要出现偶数次
解法1:利用Map 的key的唯一性
public boolean canPermutePalindromeOther1(String s) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}
int singleCount = 0;
for (Character c : map.keySet()) {
if ((map.get(c) & 1) != 0) {
singleCount++;
}
if (singleCount > 1) {
break;
}
}
return singleCount <= 1;
}
解法2:利用Set元素的唯一性
public boolean canPermutePalindromeOther2(String s) {
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if (!set.add(s.charAt(i))) {
set.remove(s.charAt(i));
}
}
return set.size() <= 1;
}
【leetcode】266. Palindrome Permutation的更多相关文章
- 【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】31. Next Permutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- ES6深入浅出-13 Proxy 与 Reflect-3.Vue 3 将用 Proxy 改写
如果说想打印出来年龄,但是有没有年龄的这个key值 把创建年龄写在一个按钮上面 通过一个事件来做. 点击创建年龄的按钮,给obj.age设置为18,但是页面的双向绑定并没有显示出来. 因为不响应式,为 ...
- 【Java】Spring之Resource(三)
Java的各种URL前缀的标准类和标准处理程序不足以完全访问低级资源.例如,没有URL可用于访问需要从类路径或相对于a获取的资源的标准化实现 ServletContext.虽然可以为专用URL 前缀注 ...
- Qt编写气体安全管理系统16-云端同步
一.前言 云端同步功能是为了后期的拓展做准备的,他的目的就是将本地的数据库中的记录,比如实时采集到的数据以及存储的运行记录等,同步到云端数据库上,默认采用阿里云的mysql数据库,阿里云速度还是挺快的 ...
- 通过直方图进行PCA准备
import graphviz import mglearn from mpl_toolkits.mplot3d import Axes3D from sklearn.datasets import ...
- Oracle 自动生成的视图VM_NSO_1
作者:Jerry 有时候优化sql的时候,在执行计划中看到有VM_NSO_X的视图,在Oracle定义中,可以吧NSO理解为nested subquery optimizing,功能就是把in转换为j ...
- 123457123456#0#----com.DoraGame.AiMiYu20--前拼后广--caimi-doraX
com.DoraGame.AiMiYu20--前拼后广--caimi-doraX
- 用VS Code写C#
目录 前言 下载SDK 安装C#支持 快速创建C#控制台 格式化代码 launch:launch.json must be configured. change 'program' to the pa ...
- 【Leetcode_easy】874. Walking Robot Simulation
problem 874. Walking Robot Simulation solution1: 思路:1)如何表示移动的方向以及移动的位置坐标; 2)障碍物坐标如何检查;3)求解的是最大距离; cl ...
- mac 下mongo的启动和关闭以及启动问题解决
原文地址:https://www.cnblogs.com/leinov/p/7341139.html mac 下mongo的启动和关闭以及启动问题解决 mongo的安装在这:http://www.cn ...
- [转载]SQL Server提权系列
本文原文地址:https://www.cnblogs.com/wintrysec/p/10875232.html 一.利用xp_cmdshell提权 xp_cmdshell默认是关闭的,可以通过下面的 ...