leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation
https://www.cnblogs.com/grandyang/p/5223238.html
判断一个字符串的全排列能否形成一个回文串。
能组成回文串,在字符串长度为偶数的情况下,每个字符必须成对出现;奇数的情况下允许一个字符单独出现,其他字符都必须成对出现。用一个set对相同字符进行加减即可。
class Solution {
public:
/**
* @param s: the given string
* @return: if a permutation of the string could form a palindrome
*/
bool canPermutePalindrome(string &s) {
// write your code here
unordered_set<char> container;
for(auto c : s){
if(container.find(c) != container.end())
container.erase(c);
else
container.insert(c);
}
return container.empty() || container.size() == ;
}
};
267.Palindrome Permutation II
https://www.cnblogs.com/grandyang/p/5315227.html
class Solution {
public:
/**
* @param s: the given string
* @return: all the palindromic permutations (without duplicates) of it
*/
vector<string> generatePalindromes(string &s) {
// write your code here
vector<string> res;
string mid = "";
int number = ;
unordered_map<char,int> m;
for(auto c : s)
m[c]++;
for(auto& it : m){
if(it.second % == )
mid += it.first;
it.second /= ;
number += it.second;
if(mid.size() > )
return res;
}
generatePalindromes(m,number,res,mid,"");
return res;
}
void generatePalindromes(unordered_map<char,int> m,int number,vector<string>& res,string mid,string out){
if(out.size() == number){
res.push_back(out + mid + string(out.rbegin(),out.rend()));
return;
}
for(auto& it : m){
if(it.second > ){
it.second--;
generatePalindromes(m,number,res,mid,out + it.first);
it.second++;
}
}
}
};
leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II的更多相关文章
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
随机推荐
- Linux操作系统的进程管理
Linux操作系统的进程管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.进程相关概念 1>.进程概述 内核的功用: 进程管理.文件系统.网络功能.内存管理.驱动程序. ...
- Python的csv文件(csv模块)和ini文件(configparser模块)处理
Python的csv文本文件(csv模块)和ini文本文件(configparser模块)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.csv文件 1>.CSV文件 ...
- Python +appium 封装desired_caps模块
使用python+appium做android的自动化测试时,首先需要启动appium服务,然后连接上手机,配置如下: desired_caps = {"platformName" ...
- centos 7 修改密码
linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于centos7环境进行操作,由于centos的版本是有差异的,继续之前请确定好版本. 操作步骤 一.重启系统,在开机过程中,快速 ...
- C#Winform使用CefSharp将WebBowser控件替换为Chrome内核
先废话一段 ,好久没写博客了,也是跟环境工作有关,之前做技术,天天博客园的翻着 (还是喜欢博客园,因为大家都无私分享交流啊,不像CSDN啥东西都要积分,鄙视之),现在偶尔需要个什么东西了才打开VS写写 ...
- 08 c++中运算符重载(未完成)
参考:轻松搞定c++语言 定义:赋予已有运算符多重含义,实现一名多用(比较函数重载) 运算符重载的本质是函数重载 重载函数的格式: 函数类型 operator 运算符名称(形参表列) { 重载实体 ...
- STM32L4R9使用HAL库调试IIC注意事项
STM32使用Cubemx生成的代码中,用到IIC的驱动,但是始终不能读写,因此使用逻辑分析仪,发现原本地址为0x58的写成了0x20,因此肯定是地址错了.因此,总结如下: 1.需要逻辑分析仪分析II ...
- Vue --- 指令练习
scores = [ { name: 'Bob', math: 97, chinese: 89, english: 67 }, { name: 'Tom', math: 67, chinese: 52 ...
- 4、NameNode启动过程详解
NameNode 内存 本地磁盘 fsimage edits 第一次启动HDFS 格式化HDFS,目的就是生成fsimage start NameNode,读取fsimage文件 start Data ...
- 20199302《Linux内核原理与分析》第十二周作业
ShellShock攻击实验 什么是ShellShock? Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许 ...