266 Palindrome Permutation
来源:互联网 发布:golang 毫秒时间戳 编辑:程序博客网 时间:2024/06/04 23:31
题目看懂了就很简单,最简单的理解就是统计string里字母出先的次数,只允许出现奇数次的字母最多一个,否则就返回false
这是一个最基本的理解,代码很好写,一个小语法错误,几乎一遍通过。。。但是考虑的时候我想了下,应该有更简单的方法,不需要把整个string都遍历一遍,肯定有条件在中间就退出的。。。一会参考下别人的方法。。。一个朴素的,只打败15%的代码如下,有待优化
public class Solution { public boolean canPermutePalindrome(String s) { Map<Character, Integer> map = new HashMap<>(); for(char ch: s.toCharArray()){ if(!map.containsKey(ch)) map.put(ch, 0); map.put(ch, map.get(ch)+1); } int oddCount=0; for(char ch: map.keySet()){ if(map.get(ch)%2==1) oddCount++; if(oddCount==2) return false; } return true; }}
0 0
- [LeetCode 266]Palindrome Permutation
- 266 Palindrome Permutation
- [Leetcode 266] Palindrome Permutation
- 266 Palindrome Permutation
- 266 Palindrome Permutation
- Leetcode NO.266 Palindrome Permutation
- Palindrome Permutation
- Palindrome Permutation
- Palindrome Permutation
- Leetcode: Palindrome Permutation
- *LeetCode-Palindrome Permutation
- 1.4 Palindrome Permutation
- [LeetCode266]Palindrome Permutation
- [LeetCode267]Palindrome Permutation II
- Leetcode - 266. Palindrome Permutation
- Leetcode266 Palindrome Permutation
- LeetCode 266. Palindrome Permutation
- Palindrome Permutation II
- 怎样烧录D-duino(Arduino&NodeMCU)
- 51Nod - 1406 dp
- [华为OJ--C++]089-DNA序列
- bzoj 1137: [POI2009]Wsp 岛屿 (半平面交)
- 简单sql命令
- 266 Palindrome Permutation
- 【bzoj1066】[SCOI2007]蜥蜴
- 闪烁1个LED
- JVM堆内存设置原理
- 计算机程序的思维逻辑 (65)
- PAT 1078.Hashing (25)
- notepad快捷键大全
- 自定义View折线图
- 算法相关——Java排序算法之插入排序(四)