原题链接在这里: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;
}
}

类似Longest Palindrome.

跟上Palindrome Permutation II.

LeetCode Palindrome Permutation的更多相关文章

  1. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  2. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  3. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  4. [LeetCode] Palindrome Permutation I & II

    Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...

  5. [LeetCode] 266. Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...

  6. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode#267] Palindrome Permutation II

    Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  8. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

  9. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. php 从myslql里导出到excel

    //导出excel 只wps可以打开public function takexcelAction(){ $name = $this->input->get_post('name'); $i ...

  2. [转]七天学会NodeJS

    转:http://nqdeng.github.io/7-days-nodejs/ NodeJS基础 什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS, ...

  3. SPFA 的两个优化

    From NOCOW SPFA算法有两个优化算法 SLF 和 LLL: SLF:Small Label First 策略,设要加入的节点是j,队首元素为i,若dist(j)<dist(i),则将 ...

  4. 20145304 Java第三周学习报告

    20145304 <Java程序设计>第三周学习总结 教材学习内容总结 1.定义类: 类定义时使用class关键词,建立实例要使用new关键词. 代码如下: /*定义类 书上例子 衣服的型 ...

  5. 【wikioi】1017 乘积最大

    题目链接 算法:划分型DP 非常典型的一道题目,划分型DP 题目描述: 设有一个长度为N的数字串,要求选手使用K个乘号将它分成K+1个部分,找出一种分法,使得这K+1个部分的乘积能够为最大.同时,为了 ...

  6. java8 引进lamda

    就像泛型能使开发人员对数据类型进行抽象,Lambda的目的是让程序员能够对程序行为进行抽象. 你可以这样想,它能够让程序员把一段程序代码当做数据一样使用.一个方法可以像定义和使用一个变量那样的方式被定 ...

  7. UITableView常见 UI 问题总结

    一,经历 1.让 group 形式的UITableView的单元格也可以修改separatorStyle属性来设置. 2.修改group形式的UITableView的 cell 之间的间距,可以更改s ...

  8. 集成IOS 环信SDK

    集成IOS SDK 在您阅读此文档时,我们假定您已经具备了基础的 iOS 应用开发经验,并能够理解相关基础概念. 下载SDK 通过Cocoapods下载地址 不包含实时语音版本SDK(EaseMobC ...

  9. Cortex-M0(NXP LPC11C14)启动代码分析

    作者:刘老师,华清远见嵌入式学院讲师. 启动代码的一般作用 1.堆和栈的初始化: 2.向量表定义: 3.地址重映射及中断向量表的转移: 4.初始化有特殊要求的断口: 5.处理器模式: 6.进入C应用程 ...

  10. poi excel导入

    poi.jar包 import java.io.File;import java.io.FileInputStream;import java.io.IOException; import org.a ...