CC150 - 11.2
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的更多相关文章
- CC150 - 11.6
Question: Given an M x N matrix in which each row and each column is sorted in ascending order, writ ...
- CC150 - 11.5
Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...
- CC150 - 11.3
Question: Given a sorted array of n integers that has been rotated an unknown number of times, write ...
- CC150 - 11.1
Question: You are given two sorted arrays, A and B, where A has a large enough buffer at the end to ...
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- 11.8---维护x的秩(CC150)
思路:比较easy.就是借助hashset让他有序然后就能够比较节省时间了. 答案: public static int[] getRankOfNumber(int[] a, int n){ int[ ...
- 11.7---叠罗汉表演节目(CC150)
1,牛客网第一题:这其实跟找最长递增子序列是一个东西.注意的地方是,返回的是最大的dp,而不是dp[N-1]. 答案: public static int getHeight(int[] men, i ...
- 11.6---矩阵查找元素(CC150)
思路,一旦提到查找就要想到二分查找. public static int[] findElement(int[][] a, int n, int m, int key) { // write code ...
- 11.5---含有空字符串的字符串查找(CC150)
注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...
随机推荐
- django-cms 代码研究(五)深入(代码结构)
前言: 前戏已经做得比较充分了,下面我们开始步入正题. 代码结构: cms |--admin (猜测是admin界面的二次开发和改良) |--cache (猜测是缓存机制的处理) |--extensi ...
- github student pack中的digital ocean可以使用银联卡支付
申请了 github student pack却因为一直没有visita信用卡,而无法使用digital ocean的 $50,一直到今天,用中国银行借记卡成功支付. 方法是: (1)注册paypal ...
- Java锁之自旋锁详解
锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized 和 ReentrantLock等等 ) .这些已经写好提供的锁为我们开发提供了便利,但是锁的具体性质以及类 ...
- Mybatis 动态sql标签
1.动态SQL片段 通过SQL片段达到代码复用 <!-- 动态条件分页查询 --> <sql id="sql_count"> ...
- GLSL的qualifier
uniform:从应用程序到vertex shader 到fragment shader都能使用,但是值一直不变: varying:从vertex shader到fragment shader,在fr ...
- How to Optimize Battery Health?
1. click on the battery icon from taskbar next to the date and time. 2. click "More power optio ...
- Find them, Catch them(poj 1703)
题目大意: 在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙输入D x y代表x于y不在一个团伙里输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙里 思路 ...
- C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)
#include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...
- SQL 总汇
/* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...
- javascript栈的建立样码
早上参加小孩的一年级入学前,看看相关的东东啦.. function Stack() { var items = []; this.push = function(element){ items.pus ...