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的更多相关文章

  1. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  2. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  3. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. 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 ...

  6. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  7. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

  8. 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 ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

随机推荐

  1. linux查看磁盘分区

    df  查看磁盘分区使用状况 用法:df [选项]... [文件]... Show information about the file system on which each FILE resid ...

  2. python编程之字符bytes转换str问题

    之前学习bytes转换str的时候,场景比较简单,这次是python中使用subprocess模块输出Linux下的执行命令的结果,默认是bytes类型,因此输出的结果并不是我们想要的,bytes的输 ...

  3. Linux-普通用户切换为root身份

    Linux 下普通用户免密码切换 root 用户 具体步骤: 1)编辑 su 文件打开第四行注释 # vim /etc/pam.d/su 2)将用户添加到 wheel 组 # usermod -G w ...

  4. Java 返回字符串中第一个不重复字符的下标 下标从0开始

    比如abcdefgabdef 其中字符c和g不重复,返回c的小标,下标从0开始,那么是2 package com.example.demo; import org.testng.annotations ...

  5. SQL进阶系列之8EXISTS谓词的用法

    写在前面 支撑SQL和关系数据库的基础理论:数学领域的集合论和逻辑学标准体系的谓词逻辑 理论篇 什么是谓词?谓词是返回值为真值(true false unknown)的函数 关系数据库里,每一个行数据 ...

  6. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  7. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. windows下面,PHP如何启动一些扩展功能

    我今天在试这个时,发现php有些默认设置,是需要人为介入修改的. 比如,当我们在安装一个软件,而这个软件需要启用php一些扩展功能. 那么,按一般套路,将php.ini文件里的相关行的注释去掉即可. ...

  9. 微信之获取微信的openid(二)详细版

    第一步 :配置测试号,网页授权获取用户基本信息. 该授权回掉页面域名为ngrok 映射的域名,我的映射地址是127.0.0.1:8080. 到此微信配置完毕,接下来就是直接上代码了 2.用户同意授权 ...

  10. 项目Beta冲刺(团队)-凡事预则立

    所属课程 软件工程1916|W(福州大学) 作业要求 项目Beta冲刺(团队)-凡事预则立 团队名称 基于云的胜利冲锋队 作业目标 为 Beta 冲刺规划安排 1.讨论组长是否重选的议题和结论 由于我 ...