LeetCode Palindrome Permutation
2016-03-11 12:20
363 查看
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/
题目:
Given a string, determine if a permutation of the string could form a palindrome.
For example,
题解:
看能否配对出现.
Time Complexity: O(n). Space: O(n).
AC Java:
可以用bitMap
跟上Palindrome Permutation II.
题目:
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++){ if(!hs.contains(s.charAt(i))){ hs.add(s.charAt(i)); }else{ hs.remove(s.charAt(i)); } } return hs.size() == 1 || hs.size() == 0; } }
可以用bitMap
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; } }
跟上Palindrome Permutation II.
相关文章推荐
- 如果你已经这样了,那你必须要跳槽
- Eclipse工作空间(workspace)打不开
- Could not transfer artifact org.springframework:spring-tx:jar:3.2.3.RELEASE
- Java 容器总结
- google maps js v3 api教程(3) -- 创建infowindow
- ios 去除按钮的按下效果(阴影)
- 简单选择排序--Java实现
- CAS Ticket票据:TGT、ST、PGT、PT、PGTIOU
- LIS O(nlogn)
- BufferedReader和InputStreamReader的用法
- 【继承与多态】C++:继承中的赋值兼容规则,子类的成员函数,虚函数(重写),多态
- Java RMI之HelloWorld篇
- 7.C语言之指针
- 推荐引擎相关
- 从 NSURLConnection 到 NSURLSession
- 关于activity转场动画makeSceneTransitionAnimation没效果的问题的解决办法
- Injection for Xcode:成吨的提高开发效率
- android线程消息传递机制——Looper,Handler,Message
- google maps js v3 api教程(3) -- 创建infowindow
- git 配置