看类容 ,:P我要回复 我要学习 ,我只看到了JavaSE之List集合操作。我写的也算吧- import java.util.*;
- public class testa {
- public static int perfect(String s) {
- int result = 0;
- s = s.toLowerCase();
- char[] array = s.toCharArray();
- Map<Character, Integer> map = new HashMap<Character, Integer>();
- for (char c : array) {
- if (map.containsKey(c)) {
- int value = map.get(c);
- map.put(c, ++value);
- } else {
- map.put(c, 1);
- }
- }
- List<Integer> list = new ArrayList<Integer>();
- Iterator<Character> iter = map.keySet().iterator();
- while (iter.hasNext()) {
- Character key = iter.next();
- list.add(map.get(key));
- }
- Collections.sort(list);
- int p = 26;
- for (int i = list.size() - 1; i >= 0; i--, --p) {
- result += list.get(i) * p;
- }
- return result;
- }
- public static void main(String args[]) {
- String str = "dad";
- System.out.println(new testa().perfect(str));
- }
- }
复制代码 我要 |