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 ...
随机推荐
- Python入门篇-匿名函数
Python入门篇-匿名函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.匿名函数概述 1>.什么是匿名函数 匿名,即没有名字 匿名函数,即没有名字的函数 2>. ...
- Oid 类
参考地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.oid?redirectedfrom=MSD ...
- dockerhub下载加速
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f5dad4ec.m.daocloud.io syste ...
- 使用selenium三种方式打开文件:
#路径读取方式一:# b.get(r"C:\我的代码\selenium自动化测试\test.html")#路径读取方式二:# b.get("C:\\我的代码\\selen ...
- Stirling数入门
第一类Stirling数 定义 $$\begin{aligned}(x)_n & =x(x-1)...(x-n+1)\\&= s(n, 0) + s(n,1)x +..+s(n,n)x ...
- 基于Ubuntu1604+ROS-kinetic+roscpp的激光雷达定位算法从零开始移植
调试的过程太麻烦了,因此打算详细解释一下每步的含义,很多地方懂了之后发现其实很简单,但是学起来却发现很多地方无从下手,因为资料太少了,真的都是不断踩坑一点一点摸索出来的,写以此文以便后人乘凉 此处将展 ...
- Window IDEA开发工具 杀死指定端口 cmd 命令行 taskkill
Windows平台 两步方法 : 1 查询端口占用,2 强行杀死进程 netstat -aon|findstr "8080" taskkill /pid 4136-t -f ...
- WinDbg常用命令系列---日志操作相关命令log*
.logopen (Open Log File) .logopen命令将事件和命令的副本从调试器命令窗口发送到新的日志文件. .logopen [Options] [FileName] .logope ...
- urql 高度可自定义&&多功能的react graphql client
urql 是一个很不错的graphql client,使用简单,功能强大,通过exchanges 实现了完整的自定义特性 通过urql 的exchanges 我们可以实现灵活的cache策略 参考资料 ...
- Tomcat启动问题:严重[main] org.apache.catalina.core.AprLifecycleListener.init An incompatible version...
今天观察tomcat启动日志,有一些以前没注意到的信息: 严重 [main] org.apache.catalina.core.AprLifecycleListener.init An incompa ...