Palindrome Permutation
2016-06-06 05:48
162 查看
通过奇偶数来判断回文,很好。
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
int oddCount = 0;
Map<Character, Boolean> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
if (map.get(c)) {
map.put(c, false);
oddCount++;
} else {
map.put(c, true);
oddCount--;
}
} else {
map.put(c, false);
oddCount++;
}
}
int mod = s.length() % 2;
if (mod == 0) {
if (oddCount == 0) {
return true;
} else {
return false;
}
} else {
if (oddCount == 1) {
return true;
} else {
return false;
}
}
}
}
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null || s.length() < 2) {
return true;
}
int oddCount = 0;
Map<Character, Boolean> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (map.containsKey(c)) {
if (map.get(c)) {
map.put(c, false);
oddCount++;
} else {
map.put(c, true);
oddCount--;
}
} else {
map.put(c, false);
oddCount++;
}
}
int mod = s.length() % 2;
if (mod == 0) {
if (oddCount == 0) {
return true;
} else {
return false;
}
} else {
if (oddCount == 1) {
return true;
} else {
return false;
}
}
}
}
相关文章推荐
- 坚刚不可夺其志
- System and device programming-Asynchronous input/output
- 字典树+DFS____字符矩阵(2015swust院赛)
- 定时任务服务开发笔记(2)——开发Web端任务管理页面
- 暴力____Football(2016swust校赛)
- Reverse Vowels of a String
- 二分_Mode(2016swust校赛)
- 运行WordCount程序
- JavaScript 可配置函数与柯里化
- 第二次冲刺个人工作总结12
- Android Studio主题设置、颜色背景配置
- SDK 第五章 绘图基础
- SDK 第四章 文本输出
- SDK 第三章 窗口与消息
- 在MapReduce中连接Hbase数据库
- [Redux] Using mapDispatchToProps() Shorthand Notation
- JavaScript DFT 离散傅里叶变换
- Activiti 数据库表信息
- [Redux] Using withRouter() to Inject the Params into Connected Components
- Ubuntu 16.04 使用useradd添加用户后没有家目录的解决方法