【LeetCode】266. Palindrome Permutation 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/palindrome-permutation/
题目描述
Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code"
Output: false
Example 2:
Input: "aab"
Output: true
Example 3:
Input: "carerac"
Output: true
题目大意
给定一个字符串,判断该字符串中是否可以通过重新排列组合,形成一个回文字符串。
解题方法
字典
回文串中,左右必须对称,因此出现次数为奇数的字符最多只有一个。
使用一个字典,保存每个字符出现的次数。如果出现次数为奇数个的字符<=1,那么可以构成回文串。
C++代码如下:
class Solution {
public:
bool canPermutePalindrome(string s) {
unordered_map<char, int> m_;
for (char c : s) {
m_[c]++;
}
int odd_count = 0;
for (auto& it : m_) {
if (it.second & 1)
odd_count++;
}
return odd_count <= 1;
}
};
日期
2019 年 9 月 17 日 —— 听了hulu宣讲会,觉得hulu的压力不大
【LeetCode】266. Palindrome Permutation 解题报告(C++)的更多相关文章
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [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 (回文排列)$
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode#266] Palindrome Permutation
Problem: Given a string, determine if a permutation of the string could form a palindrome. For examp ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- seqtk抽取测序数据
做数据比较的时候,由于同一个样本测序数据量不一致,需要抽取数据,控制数据量基本一致. 自己写脚本速度较慢,后面发现一个不错的工具:seqtk 原始数据抽取 如果只控制原始数据量一致,过滤低质量数据后直 ...
- rabbit mq的安装
rabbit mq的安装分为window的安装和linux的安装. window的安装: 1,需要安装 安装Erlang 下载地址http://www.erlang.org/downloads 我选 ...
- Shell 分发脚本
目录 Shell分发脚本 原理 rsync命令分析 特点 基本语法 实现 需求 环境变量 脚本实现 知识点 获得当前路径的目录dirname 获得当前路径的文件名basename shell远程执行命 ...
- adhere, adjust, adjacent
adhere to stick,不是to here. 在古英语里,stick是twig(细树枝).fasten(想必是用twig来固定).后引申为粘住.stick还有stab, pierce的意思,想 ...
- Linux网络(网络模型和收发流程)
网络模型 为了解决网络互联中异构设备的兼容性问题,并解耦复杂的网络包处理流程,国际标准化组织制定的开放式系统互联通信参考模型(Open System Interconnection Reference ...
- Azkaban(一)【集群安装】
目录 一.下载解压 二. 配置Mysql 三. 配置Azkaban Executor 四. 配置Azkaban WebServer 一.下载解压 1.下载地址:https://github.com/a ...
- LeetCode382-链表随机节点
原题链接:[382. 链表随机节点]:https://leetcode-cn.com/problems/linked-list-random-node/ 题目描述: 给定一个单链表,随机选择链表的一个 ...
- 手淘lib-flexible布局适配方案
前置知识:什么是rem CSS3新增的一个相对单位rem(root em,根em).rem是相对于根节点(或者是html节点).如果根节点设置了font-size:10px;那么font-size:1 ...
- Linux FTP的主动模式与被动模式
Linux FTP的主动模式与被动模式 一.FTP主被动模式 FTP是文件传输协议的简称,ftp传输协议有着众多的优点所以传输文件时使用ftp协议的软件很多,ftp协议使用的端口是21( ...
- JS - 事件常用
问:什么是事件? 答:JS创建动态页面,可以被JS侦测到的行为.网页中的每个元素都可以产生某些可以触发JS函数的事件.比如说,当用户点击按钮时,就发生一个鼠标单击(onclick)事件,需要浏览器做出 ...