方法1
class Solution {
public int longestPalindrome(String s) {
if(s == null || s.length() == 0){
return 0;
}
HashSet<Character> hashset = new HashSet<Character>();
int count = 0;
//char a[] = s.toCharArray();
for(int i = 0; i < s.length();i++){
if(!hashset.contains(s.charAt(i))){
hashset.add(s.charAt(i));
}else{
hashset.remove(s.charAt(i));
count++;
}
}
return hashset.isEmpty() ? count * 2: count * 2 + 1;
}
}
几秒前 | 通过 | 11 ms | 39.8 MB | Java |
方法2
class Solution {
public int longestPalindrome(String s) {
if(s == null || s.length() == 0){
return 0;
}
HashSet<Character> hashset = new HashSet<Character>();
int count = 0;
char a[] = s.toCharArray();
for(int i = 0; i < a.length;i++){
if(!hashset.contains(a[i])){
hashset.add(a[i]);
}else{
hashset.remove(a[i]);
count++;
}
}
return hashset.isEmpty() ? count * 2: count * 2 + 1;
}
}
提交时间 | 提交结果 | 执行用时 | 内存消耗 | 语言 |
---|---|---|---|---|
几秒前 | 通过 | 7 ms | 39.7 MB | Java |