Open Shawngbk opened 7 years ago
剩下的元素只能为1个或者0个,若是0个,则每个元素为偶数,若是1个,则这个元素在回文串的中间 public class Solution { public boolean canPermutePalindrome(String s) { if(s == null) return false; HashSet set = new HashSet(); for(int i = 0; i < s.length(); i++) { if(set.contains(s.charAt(i))) { set.remove(s.charAt(i)); } else { set.add(s.charAt(i)); } } return set.size()<=1; } }
剩下的元素只能为1个或者0个,若是0个,则每个元素为偶数,若是1个,则这个元素在回文串的中间 public class Solution { public boolean canPermutePalindrome(String s) { if(s == null) return false; HashSet set = new HashSet();
for(int i = 0; i < s.length(); i++) {
if(set.contains(s.charAt(i))) {
set.remove(s.charAt(i));
} else {
set.add(s.charAt(i));
}
}
return set.size()<=1;
}
}