LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/
题目:
Given a string, determine if a permutation of the string could form a palindrome.
For example,"code" -> False, "aab" -> True, "carerac" -> True.
题解:
看能否配对出现.
Time Complexity: O(n). Space: O(n).
AC Java:
public class Solution {
public boolean canPermutePalindrome(String s) {
if(s == null || s.length() <= 1){
return true;
}
HashSet<Character> hs = new HashSet<Character>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(!hs.contains(c)){
hs.add(c);
}else{
hs.remove(c);
}
}
return hs.size() == 0 || hs.size() == 1;
}
}
可以用bitMap
Time Complexity: O(n). Space: O(256).
public class Solution {
public boolean canPermutePalindrome(String s) {
if(s == null || s.length() <= 1){
return true;
}
int [] map = new int[256];
for(int i = 0; i<s.length(); i++){
map[s.charAt(i)]++;
}
int count = 0;
for(int i = 0; i<256; i++){
if(count == 0 && map[i]%2 == 1){ //第一次出现frequency为奇数的char
count++;
}else if(map[i] % 2 == 1){ //第二次出现frequency为奇数的char
return false;
}
}
return true;
}
}
LeetCode Palindrome Permutation的更多相关文章
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
- [LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- ai seek
原文地址链接:http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-seek--gamedev- ...
- gulp完全开发指南 => 快来换掉你的Grunt吧
最近一直在构建Angular应用,通过bower管理前端包依赖,然后通过gulp和它配合.发现gulp相比于grunt真的很轻,现在我的项目中已经取代了grunt.这里把我的一些实践贴记录下来和大家分 ...
- sdoi 2009 & 状态压缩
是不是平时在手机里玩吃豆豆游戏玩腻了呢?最近MOKIA手机上推出了一种新的围豆豆游戏,大家一起来试一试吧. 游戏的规则非常简单,在一个N×M的矩阵方格内分布着D颗豆子,每颗豆有不同的分值Vi.游戏者可 ...
- topcoder SRM 622 DIV2 FibonacciDiv2
关于斐波那契数列,由于数据量比较小, 直接打表了,代码写的比较戳 #include <iostream> #include <vector> #include <algo ...
- iOS单例的作用和使用
单例 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 一是某个类只能有一个实例:二是它必须自行创建这个实例:三是它必须自行 ...
- JS类的封装及实现代码
js并不是一种面向对向的语言, 没有提供对类的支持, 因此我们不能像在传统的语言里那样 用class来定义类, 但我们可以利用js的闭包封装机制来实现js类, 我们来封装一个简的Shape类. 1. ...
- JSP 基础概念归纳 5分钟看完
1. 符合 j2ee 标准的 web-app 的目录结构 WEB-INF classes web.xml lib servlet 开发过程 从 httpservlet 继承, 重写 doget / d ...
- 十、ios 模态窗口[实例]
一.模态窗口概念 对话框一般分为两种类型:模态类型( modal )与非模态类型( modeless ).所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话 ...
- jQuery $(document).ready()和window.onload
jQuery $(document).ready()和window.onload 根据ready()方法的API说明http://api.jquery.com/ready/. 这个方法接收一个func ...
- TF-IDF算法
转自:http://www.cnblogs.com/eyeszjwang/articles/2330094.html TF-IDF(term frequency–inverse document fr ...