Question:

Write a method to sort an array of strings so that all the anagrams are next to each other.
 package POJ;

 import java.util.Arrays;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List; public class Main { /**
*
* 11.2 Write a method to sort an array of strings so that all the anagrams are next to each other.
*
*/
public static void main(String[] args) {
Main so = new Main();
} public void sort(String[] list) {
Hashtable<String, LinkedList<String>> ht = new Hashtable<String, LinkedList<String>>();
AnagramCompare ac = new AnagramCompare();
for (String s : list) {
String key = ac.sortChars(s);
if (!ht.contains(key)) {
ht.put(key, new LinkedList<String>());
}
LinkedList<String> anagrams = ht.get(key);
anagrams.add(s);
}
int index = 0;
for (String s : ht.keySet()) {
LinkedList<String> tempList = ht.get(s);
for (String t : tempList) {
list[index] = t;
index++;
}
}
}
} class AnagramCompare implements Comparator<String> {
public String sortChars(String s1) {
char[] temp = s1.toCharArray();
Arrays.sort(temp);
return new String(temp);
} @Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return sortChars(o1).compareTo(sortChars(o2));
}
}

注意:comparator的重写方法!!!

CC150 - 11.2的更多相关文章

  1. CC150 - 11.6

    Question: Given an M x N matrix in which each row and each column is sorted in ascending order, writ ...

  2. CC150 - 11.5

    Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...

  3. CC150 - 11.3

    Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...

  4. CC150 - 11.1

    Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...

  5. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  6. 11.8---维护x的秩(CC150)

    思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ...

  7. 11.7---叠罗汉表演节目(CC150)

    1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ...

  8. 11.6---矩阵查找元素(CC150)

    思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...

  9. 11.5---含有空字符串的字符串查找(CC150)

    注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...

随机推荐

  1. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

    腾讯新浪通过IP地址获取当前地理位置(省份)的接口  腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...

  2. 怎样取出cobbler kopts中设置的参数?

    Is there a way to find out with what parameters did the kernel boot? For example if I specify noexec ...

  3. mysql show

    1.show命令语法 SHOW {BINARY | MASTER} LOGS SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset, ...

  4. 有时间测试dism

    dism /capture-image /imagefile:d\win.win /capturedir:c:\ /name:win81 dism /export-image /winboot /so ...

  5. 运输装备(codevs 1669)

    1669 运输装备  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 德国放松对英国的进攻后,把矛头指向 ...

  6. Android Session

    AsyncHttpClient保持session http://blog.csdn.net/ewrfedf/article/details/21968511

  7. php 用面向对象的方法对数据库增删改查

    主页面 <body> <h1>主页面</h1> <table width="100%" border="1" cell ...

  8. 菜鸟学Linux命令:chmod命令和数字文件权限

    chmod是一条在Unix系统中用于控制用户对文件的权限的命令(change mode单词前缀的组合)和函数.只有文件所有者和超级用户可以修改文件或目录的权限.可以使用绝对模式,符号模式指定文件的权限 ...

  9. repo的用法

    转自:http://blog.csdn.net/junglyfine/article/details/6299636 注:repo只是google用Python脚本写的调用Git的一个脚本,主要是用来 ...

  10. CI中自定义SQL查询,LIKE模糊查询的处理

    参考: /** * 据用户输入的关键字查询相册信息;照片墙搜索框功能 * @param $keyWord 关键字 * @param $pageNum 页码,第几页 * @param $pageSize ...