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: ...
随机推荐
- SVN安装与配置 SVN整合MyEclipse
SVN安装: 1.安装服务器 ######### 安装文件:SVN服务器############### # http://www.collab.net/downloads/subversion # C ...
- 【云计算】docker run详解
Docker学习总结之Run命令介绍 时间 2015-01-21 17:06:00 博客园精华区 ...
- 【SpringMVC】SpringMVC系列14之SpringMVC国际化
14.SpringMVC国际化 14.1.概述 14.2.用户切换选择语言
- ffplay 2.5.3 媒体播放器
下载地址 http://pan.baidu.com/s/1bnlMYB1 一定要解压到 D:\ffmpeg\ 目录下 双击 OpenWith_FFPlay.reg 注册ffplay 在视频文件名上面, ...
- Java for LeetCode 058 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【python】2048
来源:https://www.shiyanlou.com/courses/368 实验楼的2048程序,在linux下可实现通过终端游戏. 主要学习的知识点: 1.状态机函数实现,用字典将状态和函数相 ...
- iOS7隐藏顶部状态栏
找到工程中的Supporting Files/工程名-info.plist 添加设置 1.status bar is initially hidden=YES 2.View Controller-ba ...
- fork
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> ...
- 自定义view实现水波纹效果
水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...
- centos7下yum安装mysql
CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件,需要去官网上下载 # wget http://dev.mysql.com/get/mysql-communit ...